做科技公司的网站公司,wordpress tag 搜索,家教网站制作,国外网站网站命名空间
顶层的 cxx::bridge 属性宏接受一个可选的命名空间参数#xff0c;用于控制生成外部 Rust 项的 C 命名空间#xff0c;以及期望找到外部 C 项的命名空间。
#[cxx::bridge(namespace path::of::my::company)]
mod ffi {extern Rust {type…命名空间
顶层的 cxx::bridge 属性宏接受一个可选的命名空间参数用于控制生成外部 Rust 项的 C 命名空间以及期望找到外部 C 项的命名空间。
#[cxx::bridge(namespace path::of::my::company)]
mod ffi {extern Rust {type MyType; // 生成到 path::of::my::company::MyType}extern C {type TheirType; // 引用 path::of::my::company::TheirType}
}此外可以在桥接模块中的任何 extern 块或单个项上使用 #[namespace “…”] 属性。如果某个项没有指定命名空间它将继承其所在的 extern 块指定的命名空间如果有的话否则将继承顶层 cxx::bridge 属性指定的命名空间如果有的话否则将使用全局命名空间。
#[cxx::bridge(namespace third_priority)]
mod ffi {#[namespace second_priority]extern Rust {fn f();#[namespace first_priority]fn g();}extern Rust {fn h();}
}上述代码将生成函数 ::second_priority::f、::first_priority::g 和 ::third_priority::h。
rust_name, cxx_name
有时你希望函数或类型的 Rust 名称与其 C 名称不同。重要的是这允许使用不同的 Rust 名称绑定同一个 C 函数名的多个重载。
复制
#[cxx::bridge]
mod ffi {unsafe extern C {#[rust_name i32_overloaded_function]fn cOverloadedFunction(x: i32) - String;#[rust_name str_overloaded_function]fn cOverloadedFunction(x: str) - String;}
}#[rust_name “…”] 属性替换了 Rust 应该使用的函数名称而类似的 #[cxx_name “…”] 属性替换了 C 应该使用的名称。
这两个属性中的任何一个都可以用于 extern “Rust” 和 extern “C” 函数具体取决于你在上下文中认为哪个更清晰。
相同的属性适用于重命名函数、不透明类型、共享结构体和枚举以及枚举变体。