Is there any good way to represent a pointer hierarchy in a static context?
I'm working with a FFI that requires you to define a static struct for registration. That struct has a pointer to an array of pointers (null terminated), which each points to another struct. All in all, the rust representation looks about like this:
#![allow(non_camel_case_types)]
#[repr(C)]
struct inner_c_struct {
a: *mut i32,
}
#[repr(C)]
struct outer_c_struct {
arr: *mut *mut inner_c_struct,
}
static mut VAL: i32 = 100;
static INNER: inner_c_struct = inner_c_struct {
a: unsafe { &mut VAL },
};
static ARR: [*mut inner_c_struct; 2] = [&INNER, std::ptr::null_mut()];
// This is the important part
static OUTER: outer_c_struct = outer_c_struct {
arr: ARR.as_mut_ptr(),
};
That doesn't compile of course, since pointers aren't sync.But what is the best way to make this compile?
I've been using a UnsafeSyncCell<T>(UnsafeCell<T>)
wrapper with Sync
and Send
that I"ve been using to wrap everything (so ARR: [UnsafeSyncCell<*mut T>; 2]
and OUTER: UnsafeSyncCell<outer_c_struct>
), but it is quite tedious to work with, with conversions among all the different pointer types.
Is there a better way to manage this?