cxx in ChromiumThe cxx crate is the current recommendation for C++/Rust interop in Chromium. This document provides advice for working with cxx in Chromium.
For introductory guidance, see the cxx chapter in the Chromium section of the Comprehensive Rust course. See ffi.md for an overview of FFI tools and general FFI guidance.
cxx has some rough edges. Our plan for addressing them is to recommend Crubit in the future, once its experimental support graduates out of the teamfood stage (follow https://crbug.com/351793625 to keep track of Crubit work).
Make sure you're familiar with our general FFI safety considerations and safety guidelines. In addition, keep the following cxx-specific details in mind:
cxx::bridge, it is treated as opaque, zero-sized placeholder type from the perspective of the Rust compiler. This means that it must always be behind an indirection (a pointer, reference, box, etc.), and the Rust compiler makes no assumptions about what it points to.#[cxx::bridge] declaration. cxx supports reusing types across multiple bridges, but there are some rough edges.namespace. For example: #[cxx::bridge(namespace = "some_cpp_namespace")].cxx can't translate), create wrapper functions in a C++ shim file._shim.h (or .cc).#[cxx::bridge] #includes in the deps of the rust_static_library. cxx doesn‘t parse these headers, so neither cxx nor a plain gn check reports missing dependencies, and missing dependencies can cause flaky builds (e.g. when a generated header isn’t generated yet). To find them, build the target, and then run gn check <out_dir> --check-generated //path/to:<target>_cxx_generated (gn check skips generated files that don't exist yet, and it checks the generated C++ code only as part of the ..._cxx_generated target).These are not necessarily best practices, but take them into consideration when designing your bridges.
cxx's [attributes] to generate more idiomatic code:Self attribute can attach static functions to existing types.rust_name attribute is useful for translating between naming conventions.extern "C++" block as unsafe.In general, you should follow idomatic Rust style, but some idioms are especially useful in an FFI context:
Use From (or TryFrom) to convert between types; it's especially well-suited to converting between types that are logically equivalent, such as FFI types like ffi::ColorType and third-party crate types like png::ColorType.
See an example trait implementation and an example of using the conversion as foo.into().
Note that when implementing the conversion for types defined in other crates, you may need to work around the orphan rule by implementing Into (or TryInto) trait instead.
Use the ? operator alongside the Option or Result types to check for errors.
When used in the FFI layer, this may require splitting some functions into (a) one that returns Result<T, E> and uses ? sugar, and (b) one that translates Result<T, E> into FFI-friendly status.
There are some examples here and here. This example avoids having to come up with a separate name by using an anonymous function.
Use the let Ok(foo) = ... else { ... } syntax to handle errors when you don't always want to return if you see a None. See an example here.