Navigate:
~$CXX0.1%

CXX: Safe interoperability between Rust and C++

Safe FFI library for calling C++ from Rust and vice versa with zero-overhead bindings via code generation.

LIVE RANKINGS • 02:15 PM • STEADY
OVERALL
#345
146
DEVELOPER TOOLS
#72
33
30 DAY RANKING TREND
ovr#345
·Devel#72
STARS
6.7K
FORKS
397
7D STARS
+8
7D FORKS
0
See Repo:
Share:

Learn more about CXX

CXX is a foreign function interface (FFI) library that facilitates safe interoperability between Rust and C++ codebases. It works by defining FFI boundaries within Rust modules using a bridge macro, then performing static analysis on both sides to ensure type safety and ABI compatibility. The library uses dual code generators - a Rust procedural macro and a C++ code generator - to emit the necessary extern "C" signatures and static assertions. CXX supports native types from both languages including Rust's String/Vec/Box and C++'s std::string/std::vector/std::unique_ptr, making it suitable for integrating existing C++ libraries into Rust applications or vice versa.

CXX

1

Static Safety Analysis

Performs compile-time verification of FFI boundaries to uphold both Rust's and C++'s type system invariants. Uses static assertions to ensure signature accuracy between languages.

2

Zero-Overhead Bindings

Generates ABI-compatible signatures without runtime checks, memory allocation, copying, or serialization. Operates at native performance levels.

3

Native Type Support

Provides built-in bindings for standard library types from both languages with idiomatic APIs. Supports complex types like smart pointers and containers across the FFI boundary.


#[cxx::bridge]
mod ffi {
    // Shared struct visible to both Rust and C++
    struct BlobMetadata {
        size: usize,
        tags: Vec<String>,
    }

    extern "Rust" {
        // Opaque Rust type
        type MultiBuf;
        // Rust function callable from C++
        fn next_chunk(buf: &mut MultiBuf) -> &[u8];
    }

    unsafe extern "C++" {
        include!("blobstore.h");
        // Opaque C++ type
        type BlobstoreClient;
        // C++ functions callable from Rust
        fn new_blobstore_client() -> UniquePtr<BlobstoreClient>;
        fn put(&self, parts: &mut MultiBuf) -> u64;
        fn metadata(&self, blobid: u64) -> BlobMetadata;
    }
}


v1.0.194

1.0.194

  • Drop Bazel 7 (#1696, #1697)
v1.0.193

1.0.193

  • Support for Bazel 9.0.0 (#1693, #1695)
v1.0.192

1.0.192

  • Add support for `Vec<Box<T>>` (#1681, )


[ EXPLORE MORE ]

Related Repositories

Discover similar tools and frameworks used by developers