Rust Bridge Declarations

#[cxx::bridge] mod ffi { extern "Rust" { type MyType; // Opaque type fn foo(&self); // Method on `MyType` fn bar() -> Box<MyType>; // Free function } } struct MyType(i32); impl MyType { fn foo(&self) { println!("{}", self.0); } } fn bar() -> Box<MyType> { Box::new(MyType(123)) }

Speaker Notes

  • extern "Rust" 中声明的内容引用了父级模块中作用域内的内容。
  • CXX 代码生成器使用 extern "Rust" 部分生成包含相应 C++ 声明的 C++ 头文件。生成的头文件与包含桥接的 Rust 源文件的路径相同,但文件扩展名为 .rs.h。