Thiago Pontes
Thiago Pontes

Reputation: 606

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

I have the following C code:

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

After compiling a .so from this code I can load this symbol with dlsym and get the contents of Vmod_Spec and iterate over it. How can I achieve the same result exposing a symbol like this one from Rust?

Upvotes: 0

Views: 336

Answers (1)

Thiago Pontes
Thiago Pontes

Reputation: 606

The Rust equivalent for this would be exposing a [*const c_char;3] as an static value. The problem is that if you declare your value like this you will get an error: error: the trait core::marker::Sync is not implemented for the type *const i8 [E0277]. And you can't implement this trait for *const c_char because you don't own this type. The workaround was to declare a wrapper type around *const c_char and use it instead:

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

And then I'll have a Vmod_Spec symbol which points to an array of values.

Upvotes: 1

Related Questions