src/chromium/interoperability-with-cpp.md
The Rust community offers multiple options for C++/Rust interop, with new tools being developed all the time. At the moment, Chromium uses a tool called CXX.
You describe your whole language boundary in an interface definition language (which closely resembles Rust) and then CXX tools generate declarations for functions and types in both Rust and C++.
See the CXX tutorial for a full example of using this.
<details>Talk through the diagram. Explain that behind the scenes, this is doing just the same as you previously did. Point out that automating the process has the following benefits:
#[cxx::bridge] doesn't match the actual C++ or Rust
definitions, but with out-of-sync manual bindings you'd get Undefined
Behavior)&[T] can be passed across the FFI boundary, even though it doesn't
guarantee any particular ABI or memory layout. With manual bindings
std::span<T> / &[T] have to be manually destructured and rebuilt out of
a pointer and length - this is error-prone given that each language
represents empty slices slightly differently)std::unique_ptr<T>, std::shared_ptr<T>, and/or Box
are natively supported. With manual bindings, one would have to pass
C-ABI-compatible raw pointers, which would increase lifetime and
memory-safety risks.rust::String and CxxString types understand and maintain differences in
string representation across the languages (e.g. rust::String::lossy can
build a Rust string from non-UTF-8 input and rust::String::c_str can
NUL-terminate a string).