I am porting a static library to Rust that will be linked with a C application which will provide a global array. Here is the definition of the struct and array:
typedef struct glkunix_argumentlist_struct {
char *name;
int argtype;
char *desc;
} glkunix_argumentlist_t;
extern glkunix_argumentlist_t glkunix_arguments[];
There is no length parameter, instead the last entry in the array will be {NULL, 0, NULL} (example), which you test for when looping through the array.
I think this is the correct Rust representation for the struct:
#[repr(C)]
struct GlkUnixArgument {
name: *const c_char,
argtype: c_int,
desc: *const c_char,
}
The Rust Nomicon shows how to define an extern for a single integer.
I've seen that if you have a length parameter you can use std::slice::from_raw_parts to get a slice, but we don't have a length yet. I could modify the C code to provide one, but I would like to be able to provide a drop-in replacement if I can.
I haven't yet seen how to just create one single Rust struct from a extern. Though I did just have the idea of just std::slice::from_raw_parts with a length of 1.
Is there a better way to more directly define an extern struct in Rust?
from_raw_parts(obviously it would be a different function) before accessing the struct fields?const size_t len = sizeof glkunix_arguments / sizeof *glkunix_arguments;