16 个版本
0.5.9 | 2023 年 8 月 16 日 |
---|---|
0.5.8 | 2023 年 3 月 30 日 |
0.5.7 | 2022 年 4 月 29 日 |
0.5.6 | 2020 年 12 月 29 日 |
0.2.1 | 2017 年 3 月 22 日 |
在 FFI 中排名 71
每月下载量 329,558
用于 274 个 crate(其中 43 个直接使用)
73KB
1.5K SLoC
rust-cpp - 直接在 Rust 中嵌入 C++ 代码
概述
rust-cpp
是一个构建工具和宏,它使你能够在 Rust 代码中内联编写 C++ 代码。
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)
此 crate 还通过自动实现诸如 Drop、Clone(如果 C++ 类型可以复制)等特质,帮助暴露一些 C++ 类到 Rust 中。
cpp_class!{
#[derive(PartialEq)]
unsafe struct MyClass as "std::unique_ptr<MyClass>"
}
使用方法
有关使用信息和详细文档,请参阅 cpp
crate 模块级别的文档。
与 cxx
crate 的区别
此 crate 允许你在 Rust 函数中内联编写 C++ 代码,而使用 cxx
crate 时,你必须编写一些样板代码,以便在另一个 .cpp
文件中声明函数的调用。
当尝试调用 C++ 库并且可能需要调用许多小片段时,内联 C++ 代码可能会有所帮助。否则,在多个地方编写和维护许多小函数的样板代码可能会很繁琐。
这些 crate 也可以一起使用。`cxx
` crate 提供了一些有用的类型,如 `CxxString
`,也可以与这个 crate 一起使用。
`cxx
` 桥接器进行了更多的类型检查,可以避免某些类别的错误。而此 crate 只能检查大小和对齐。
依赖项
~2.5–4MB
~75K SLoC