Rust Bridge Declarations

  1. #[cxx::bridge]
  2. mod ffi {
  3. extern "Rust" {
  4. type MyType; // Opaque type
  5. fn foo(&self); // Method on `MyType`
  6. fn bar() -> Box<MyType>; // Free function
  7. }
  8. }
  9. struct MyType(i32);
  10. impl MyType {
  11. fn foo(&self) {
  12. println!("{}", self.0);
  13. }
  14. }
  15. fn bar() -> Box<MyType> {
  16. Box::new(MyType(123))
  17. }
  • Items declared in the extern "Rust" reference items that are in scope in the parent module.
  • The CXX code generator uses your extern "Rust" section(s) to produce a C++ header file containing the corresponding C++ declarations. The generated header has the same path as the Rust source file containing the bridge, except with a .rs.h file extension.