【CXX】6 内置绑定

除了所有基本类型(如 i32 <=> int32_t),以下常见类型可以用于共享结构体的字段以及外部函数的参数和返回值。

Rust 中的名称 C++ 中的名称 限制条件
String rust::String
&str rust::Str
&[T] rust::Slice 不能包含不透明的 C++ 类型
&mut [T] rust::Slice 不能包含不透明的 C++ 类型
CxxString std::string 不能按值传递
Box rust::Box 不能包含不透明的 C++ 类型
UniquePtr std::unique_ptr 不能包含不透明的 Rust 类型
SharedPtr std::shared_ptr 不能包含不透明的 Rust 类型
[T; N] std::array<T, N> 不能包含不透明的 C++ 类型
Vec rust::Vec 不能包含不透明的 C++ 类型
CxxVector std::vector 不能按值传递,不能包含不透明的 Rust 类型
*mut T, *const T T*, const T* 带有裸指针参数的函数必须声明为 unsafe 才能调用
fn(T, U) -> V rust::Fn<V(T, U)> 目前仅支持从 Rust 传递到 C++
Result throw/catch 仅允许作为返回类型

C++ 中 rust 命名空间的 API 由 CXX GitHub 仓库中的 include/cxx.h 文件定义。在使用这些类型时,你需要在 C++ 代码中包含此头文件。当使用 Cargo 和 cxx-build crate 时,可以通过 #include "rust/cxx.h" 包含此头文件。

rust 命名空间还提供了表中所有类型的小写别名,适用于偏好这种风格的代码库。例如,rust::String 和 rust::Vec 也可以写成 rust::string 和 rust::vec 等。

待实现的绑定

以下类型计划在"不久的将来"支持,但目前尚未实现。这些类型的实现预计不会太困难,但需要为每种类型在其非原生语言中设计一个良好的 API。

Rust 中的名称 C++ 中的名称
BTreeMap<K, V> 待定
HashMap<K, V> 待定
Arc 待定
Option 待定
待定 std::map<K, V>
待定 std::unordered_map<K, V>
相关推荐
byxdaz10 小时前
QT运行单进程实例
qt
Wyn_13 小时前
【ZMQ/QT】Windows11 + Qt 安装配置zmq(亲测可用)
qt·zmq·windows11
864记忆14 小时前
Qt创建连接注意事项
数据库·qt·nginx
古城小栈14 小时前
Rust 泛型 敲黑板
rust
古城小栈15 小时前
Rust Trait 敲黑板
开发语言·rust
浪客川20 小时前
【百例RUST - 005】所有权和切片
开发语言·后端·rust
古城小栈20 小时前
Axum: Rust 好用的 Web 框架
开发语言·rust
古城小栈21 小时前
Rust 并发、异步,碾碎它们
开发语言·后端·rust
赵民勇21 小时前
Qt QML中Component模块详解
qt