Example Bindings

CXX requires that the whole C++/Rust boundary is declared in cxx::bridge modules inside .rs source code.

  1. #[cxx::bridge]
  2. mod ffi {
  3. extern "Rust" {
  4. type MultiBuf;
  5. fn next_chunk(buf: &mut MultiBuf) -> &[u8];
  6. }
  7. unsafe extern "C++" {
  8. include!("example/include/blobstore.h");
  9. type BlobstoreClient;
  10. fn new_blobstore_client() -> UniquePtr<BlobstoreClient>;
  11. fn put(self: &BlobstoreClient, buf: &mut MultiBuf) -> Result<u64>;
  12. }
  13. }
  14. // Definitions of Rust types and functions go here

Point out:

  • Although this looks like a regular Rust mod, the #[cxx::bridge] procedural macro does complex things to it. The generated code is quite a bit more sophisticated - though this does still result in a mod called ffi in your code.
  • Native support for C++’s std::unique_ptr in Rust
  • Native support for Rust slices in C++
  • Calls from C++ to Rust, and Rust types (in the top part)
  • Calls from Rust to C++, and C++ types (in the bottom part)

Common misconception: It looks like a C++ header is being parsed by Rust, but this is misleading. This header is never interpreted by Rust, but simply #included in the generated C++ code for the benefit of C++ compilers.