Support for casting/bridging across separate template instantiations.

This CL reworks how the `forward_declare.rs` crate supports casting
across forward declarations and complete definitions:

1. This CL introduces a new `TransmutabilityEquivalenceClass` trait
   (which provides a single associated item - the `Name` type alias).
2. The `IncompleteTransmute<Target>` trait has been made private
   and reimplemented on top of the `TransmutabilityEquivalenceClass`
   trait:

   A. Structs/unions with the same
      `TransmutabilityEquivalenceClass::Name` can be safely transmuted
      between each other.
   B. Default `impl`s define separate equivalences classes for i)
      shared/immutable references `&T`, and ii) exclusive/immutable
      references `&mut T`, and iii) `Vec<&T>`.

The changes above are sufficient to support casting across separate
template instantiations (because Crubit already generates an appropriate
`!unsafe_define` macro call for template instantiations).  This CL adds
an explicit test under `test/templates/bridging` as well as a bit of
test coverage under `test/templates/type_alias`.

For readability/reviewability the CL tries to minimize the amount of
changes (i.e. avoiding removing, renaming, or reordering items in
`forward_declare.rs`).  A TODO has been left to follow-up with a
separate CL that will clean things up.

PiperOrigin-RevId: 458234228
10 files changed
tree: 8f39ad7e164204078172b9ca100967be7e7700fe
  1. .bazelci/
  2. bazel/
  3. cc_template/
  4. common/
  5. docs/
  6. lifetime_analysis/
  7. lifetime_annotations/
  8. migrator/
  9. nullability_verification/
  10. rs_bindings_from_cc/
  11. .bazelrc
  12. .gitignore
  13. BUILD
  14. Cargo.Bazel.lock
  15. CODE_OF_CONDUCT
  16. CONTRIBUTING
  17. LICENSE
  18. README.md
  19. WORKSPACE
README.md

Crubit: C++/Rust Bidirectional Interop Tool

Build status

Extremely experimental interop tooling for C++ and Rust.

Please don‘t use, this is an experiment and we don’t yet know where will it take us. There will be breaking changes without warning. Unfortunately, we can't take contributions at this point.

Building Crubit

$ apt install clang lld bazel
$ git clone git@github.com:google/crubit.git
$ cd crubit
$ bazel build --linkopt=-fuse-ld=/usr/bin/ld.lld //rs_bindings_from_cc:rs_bindings_from_cc_impl

Using a prebuilt LLVM tree

$ git clone https://github.com/llvm/llvm-project
$ cd llvm-project
$ CC=clang CXX=clang++ cmake -S llvm -B build -DLLVM_ENABLE_PROJECTS='clang' -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install
$ cmake --build build -j
$ # wait...
$ cmake --install build
$ cd ../crubit
$ LLVM_INSTALL_PATH=../llvm-project/install bazel build //rs_bindings_from_cc:rs_bindings_from_cc_impl