如何使用 cxx 包调用 C++ 构造函数?

lap*_*tou 3 c++ ffi rust

我发现了这个问题,但它已经有3年历史了,从那时起,板条箱之类的东西cxx就出现了。现在是否可以从 Rust 构造一个 C++ 对象,或者我仍然需要创建一个垫片?

dto*_*nay 6

就构造函数按值“返回”C++ 类型而言,它们不能转换为 Rust,因为 Rust 移动 (memcpy) 与 C++ 移动(可能需要调用移动构造函数)不兼容。将任意构造函数翻译为fn new() -> Self是不正确的。

您可以使用bindgen不安全地绑定它们,它假设在没有构造函数调用的情况下移动是可以的,或者您可以使用自述文件中的“共享结构”方法,该方法可以在任何一种语言中安全地移动,或者您可以使用include!垫片在unique_ptr后面进行构造或类似的。

最后一种方法看起来像:

// suppose we have a struct with constructor `ZeusClient(std::string)`

// in a C++ header:
std::unique_ptr<ZeusClient> zeus_client_new(rust::Str arg);

// in the corresponding C++ source file:
std::unique_ptr<ZeusClient> zeus_client_new(rust::Str arg) {
  return make_unique<ZeusClient>(std::string(arg));
}

// in the Rust cxx bridge:
extern "C++" {
    include!("path/to/zeus/client.h");
    include!("path/to/constructorshim.h");

    type ZeusClient;

    fn zeus_client_new(arg: &str) -> UniquePtr<ZeusClient>;
}
Run Code Online (Sandbox Code Playgroud)

将来,CXX 很可能会包含针对此模式的内置内容,或者可能针对没有移动构造函数的结构的特殊情况。这是在dtolnay/cxx#280中跟踪的。

extern "C++" {
    type ZeusClient;

    fn new(arg: &str) -> ZeusClient;  // will verify there is no move constructor

    fn new(arg: &str) -> UniquePtr<ZeusClient>;  // okay even with move constructor
}
Run Code Online (Sandbox Code Playgroud)