无法共享C++使用CXX桥与生锈结构

发布于 2025-01-25 12:02:06 字数 1222 浏览 1 评论 0原文

我了解C ++ 必须隐藏在指针后面(*mutsharedptr< t>等)。但是结构似乎应该是“ c”。

我正在尝试导出

如果我将struct定义放在CXX桥模块的根部中,例如:

#[cxx::bridge(namespace = "RDKit")]
pub mod ffi {
    pub struct CleanupParameters {
        rdbase: String,
    }
    
    unsafe extern "C++" { /* snip */ }
}

我会收到编译器错误:

warning: ..../rdkit-sys/src/cxx/bridge/mol_standardize.rs.cc:87:10: error: definition of type 'CleanupParameters' conflicts with type alias of the same name
warning:   struct CleanupParameters;
warning:          ^

这很有意义,就像我在定义新的cleanupparameters而不是“采用” C ++定义,因此碰撞的名称很有意义。

但是,如果我试图将定义移至外部区块:

#[cxx::bridge(namespace = "RDKit")]
pub mod ffi {
    unsafe extern "C++" {   
        pub struct CleanupParameters {
            rdbase: String,
        }
    }
}

CXX Bridge Proc Macro不允许语言:

  error[cxxbridge]: expected one of: `fn`, `static`, `type`
    ┌─ src/cxx/bridge/mol_standardize.rs:7:13
    │
  7 │         pub struct CleanupParameters {
    │             ^^^^^^ expected one of: `fn`, `static`, `type`

这没有多大意义。如何将C ++的“非不透明”结构共享为Rust?

I understand C++ class definitions must be hidden behind a pointer (*mut, SharedPtr<T>, etc). But structs seem like they should be "C like".

I am trying to export CleanupParameters to the Rust side of the binding.

If I put the struct definition in the root of the CXX bridge module like:

#[cxx::bridge(namespace = "RDKit")]
pub mod ffi {
    pub struct CleanupParameters {
        rdbase: String,
    }
    
    unsafe extern "C++" { /* snip */ }
}

I get the compiler error:

warning: ..../rdkit-sys/src/cxx/bridge/mol_standardize.rs.cc:87:10: error: definition of type 'CleanupParameters' conflicts with type alias of the same name
warning:   struct CleanupParameters;
warning:          ^

which makes sense, it's like I'm defining a new CleanupParameters and not "adopting" the C++ definition, so the name collision makes sense.

But if I try to move the definition in to the extern block:

#[cxx::bridge(namespace = "RDKit")]
pub mod ffi {
    unsafe extern "C++" {   
        pub struct CleanupParameters {
            rdbase: String,
        }
    }
}

the language is not allowed by the CXX bridge proc macro:

  error[cxxbridge]: expected one of: `fn`, `static`, `type`
    ┌─ src/cxx/bridge/mol_standardize.rs:7:13
    │
  7 │         pub struct CleanupParameters {
    │             ^^^^^^ expected one of: `fn`, `static`, `type`

which does not make much sense. How can I share "non opaque" structs from C++ into Rust?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文