14 releases

0.5.7 Apr 29, 2022
0.5.6 Dec 29, 2020
0.5.5 Jul 12, 2020
0.5.4 Sep 6, 2019
0.2.1 Mar 22, 2017

#29 in FFI

Download history 32799/week @ 2022-03-11 36017/week @ 2022-03-18 36063/week @ 2022-03-25 34509/week @ 2022-04-01 33383/week @ 2022-04-08 28186/week @ 2022-04-15 29599/week @ 2022-04-22 30968/week @ 2022-04-29 43075/week @ 2022-05-06 31852/week @ 2022-05-13 35356/week @ 2022-05-20 24527/week @ 2022-05-27 33093/week @ 2022-06-03 32624/week @ 2022-06-10 28923/week @ 2022-06-17 22543/week @ 2022-06-24

123,259 downloads per month
Used in 53 crates (2 directly)

MIT/Apache

11KB
239 lines

rust-cpp - Embed C++ code directly in Rust

Build status Documentation

Overview

rust-cpp is a build tool & macro which enables you to write C++ code inline in your rust code.

let name = std::ffi::CString::new("World").unwrap();
let name_ptr = name.as_ptr();
let r = unsafe {
    cpp!([name_ptr as "const char *"] -> u32 as "int32_t" {
        std::cout << "Hello, " << name_ptr << std::endl;
        return 42;
    })
};
assert_eq!(r, 42)

The crate also help to expose some C++ class to Rust by automatically implementing trait such as Drop, Clone (if the C++ type can be copied), and others

cpp_class!{
    #[derive(PartialEq)]
    unsafe struct MyClass as "std::unique_ptr<MyClass>"
}

Usage

For usage information and in-depth documentation, see the cpp crate module level documentation.

Differences with the cxx crate

This crate allows to write C++ code "inline" within yout Rust functions, while with the cxx crate, you have to write a bit of boiler plate to have calls to functions declared in a different .cpp file. Having C++ code inline might be helpful when trying to call to a C++ library and that one may whish to make plenty of call to small snipets. It can otherwise be fastidious to write and maintain the boiler plate for many small functions in different places.

These crate can also be used in together. The cxx crate offer some usefull types such as CxxString that can also be used wth this crate.

The cxx bridge does more type checking which can avoid some classes of errors. While this crate can only check for equal size and alignement.

Dependencies

~235–630KB
~15K SLoC