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. }

10. struct MyType(i32);

12. impl MyType {
13. fn foo(&self) {
14. println!("{}", self.0);
15. }
16. }

18. fn bar() -> Box<MyType> {
19. Box::new(MyType(123))
20. }
  • 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.