A demonstration project showcasing seamless interoperability between Rust and C++ using cxx and Corrosion. This project implements a toy blobstore system where Rust provides high-performance data processing functions that are consumed by a C++ application.
- Rust-C++ FFI: Safe and efficient interoperability using the
cxx
crate - Modern Build System: CMake with Corrosion for seamless Rust integration
- Cross-Language Data Structures: Shared structs and types between Rust and C++
- Memory Safety: Zero-copy data sharing where possible
- Link Time Optimization: Optional LTO support for maximum performance
rust2cpp/
├── src/
│ ├── lib.rs # Rust FFI bridge definition
│ ├── blobstore.rs # Rust implementation
│ ├── main.cpp # C++ application entry point
│ └── blobstore.cpp # C++ blobstore client implementation
├── include/
│ └── blobstore.hpp # C++ header with class definitions
├── Cargo.toml # Rust package configuration
├── CMakeLists.txt # CMake build configuration
└── build.rs # Rust build script
- Rust: Install from rustup.rs
- CMake: Version 3.21 or higher
- C++ Compiler: Supporting C++17 (GCC, Clang, or MSVC)
- Ninja: Build system (optional but recommended)
-
Clone the repository:
git clone https://github.com/DapengFeng/rust2cpp.git cd rust2cpp
-
Configure with CMake:
cmake -B build -G Ninja
-
Build the project:
cmake --build build
-
Run the example:
./build/rust2cpp
- Shared Libraries: Use
-DBUILD_SHARED_LIBS=ON
(default) - Link Time Optimization: Use
-DENABLE_LIO=ON
(default, requires LTO support)
The project demonstrates a blobstore system with the following architecture:
- Defines the FFI bridge using
#[cxx::bridge]
- Implements
next_chunk()
function for processing multi-buffer data - Exports shared data structures like
MultiBuf
andBlobMetadata
- Implements
BlobstoreClient
class with methods:put()
: Upload data using Rust's chunk processingtag()
: Add metadata tags to blobsmetadata()
: Retrieve blob information
- Uses Rust functions seamlessly through generated bindings
auto client = org::blobstore::new_blobstore_client();
auto chunks = rust::Vec<uint8_t> {'H','e','l','l','o',',',' ','w','o','r','l','d','!'};
auto buf = org::blobstore::MultiBuf{
.chunks = std::move(chunks),
.lens = {7, 6},
.pos = 0,
.len_pos = 0
};
auto blobid = client->put(buf); // Uses Rust's next_chunk()
client->tag(blobid, "greeting"); // Add metadata
auto metadata = client->metadata(blobid); // Retrieve info
Rust:
cxx
- Safe FFI bindingscxx-build
- Build-time code generationmiette
- Error handling in build scripts
C++:
- Standard library containers (
std::vector
,std::unordered_map
) - Memory management (
std::unique_ptr
,std::shared_ptr
)
- Corrosion: Integrates Rust builds into CMake
- cxxbridge: Generates C++ headers from Rust bridge definitions
- Static Linking: Rust code compiled as static library
- Profile: Release builds with full optimizations
- Zero-Copy: Data structures shared without copying where possible
- Link Time Optimization: Cross-language LTO when supported
- Release Profile: Aggressive optimizations (
opt-level = 3
,lto = true
) - Static Linking: Eliminates runtime dependencies
- Fork the repository
- Create a feature branch
- Make your changes
- Ensure code passes linting (cpplint configuration included)
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
Copyright (c) 2025 Dapeng Feng. All rights reserved.