Crubit: C++/Rust Bidirectional Interop Tool

Docs Rust workflow GitHub

Crubit is a bidirectional bindings generator for C++ and Rust, allowing users to:

Example

Calling Rust from C++

Consider the following Rust library:

pub struct Account {
    pub id: u64,
    pub balance: f64,
}

impl std::fmt::Display for Account { ... }

// Takes shared references, mapped to const references in C++.
pub fn calculate_interest(account: &Account, rate: f64) -> f64 { ... }

// Takes a string slice, mapped to rs::StrRef in C++.
pub fn is_valid_username(username: &str) -> bool { ... }

You can call these Rust functions from C++:

#include "path/to/account.h"
#include <iostream>

void demo() {
  account::Account my_account{.id = 123, .balance = 1000.0};
  double interest = account::calculate_interest(my_account, 0.05);

  // my_account is printable because Account implements Display in Rust!
  std::cout << my_account << std::endl;

  if (account::is_valid_username("bob")) {
    std::cout << "Valid user" << std::endl;
  }
}

Calling C++ from Rust

Consider the following C++ header:

#include <string_view>
#include <optional>
#include <memory>

struct User {
  int id;
  double balance;
};

std::optional<User> FindUser(int id);
std::unique_ptr<User> CreateUser(std::string_view name, int id);

You can call these C++ functions from Rust:

use cpp_std::unique_ptr;
use ffi_11::c_int;
use user_api::{CreateUser, FindUser, User};

let id: c_int = 123;
let user: Option<User> = FindUser(id).into();
if let Some(u) = user {
    let balance: f64 = u.balance;
    println!("User {} has balance {}", u.id, balance);
}

let new_user: unique_ptr<User> = CreateUser("Alice".into(), 456);

Why Crubit?

Getting Started

1. Set up your build

Crubit runs as part of your build, so that the bindings always match the code they wrap. Pick your build system:

Build system C++ calling Rust (cpp_api_from_rust) Rust calling C++ (rust_api_from_cpp)
Bazel Supported (example) Supported
CMake Supported (example) Not yet
Other Through the cargo cpp_api_from_rust subcommand Not yet

Crubit works with nightly Rust toolchains and with the two most recent stable releases, as long as the rustc-dev component is installed (see the test matrix below). There are no binary releases yet; pin a commit of this repository. Build System Integrations covers toolchain requirements and how to set up each build system.

2. Use your Rust and C++ APIs together

Add a cpp_api_from_rust or rust_api_from_cpp target next to your library, and start calling it from the other language. Each direction has a walkthrough that explains what the generated API looks like and how to customize it, plus copy-pastable examples that include snapshots of the generated bindings:

See the full documentation at crubit.rs for additional reference material.

Test Matrix

Nightly Stable
nightly-0 stable-0
nightly-1 stable-1
nightly-2
nightly-3
nightly-4
nightly-5
nightly-6