将包含字符串数组的 C 符号从 Rust 公开到 C

Exposing a C symbol containing an array of strings from Rust to C

我有以下 C 代码:

const char * const Vmod_Spec[] = {
    "example.hello[=10=]Vmod_Func_example.hello[=10=]STRING[=10=]STRING[=10=]",
    "INIT[=10=]Vmod_Func_example._init",
    0
};

从这段代码编译 .so 后,我可以用 dlsym 加载这个符号并获取 Vmod_Spec 的内容并迭代它。我怎样才能从 Rust 中暴露出像这样的符号来获得相同的结果?

Rust 的等价物是将 [*const c_char;3] 暴露为 static 值。问题是,如果你像这样声明你的值,你会得到一个错误:error: the trait core::marker::Sync is not implemented for the type *const i8 [E0277]。而且你不能为 *const c_char 实现这个特性,因为你不拥有这个类型。解决方法是在 *const c_char 周围声明一个包装器类型并改为使用它:

struct Wrapper(*const c_char)
unsafe impl Sync for Wrapper { }
#[no_mangle]
pub static Vmod_Spec: [Wrapper; 3] = etc..

然后我将有一个指向值数组的 Vmod_Spec 符号。