I have a blah-dll
crate that gets compiled to a C DLL.
It defines an auto-generated public API like this:
// original rust code
enum MyEnum<T> {
A,
B,
C(T),
}
struct MyType {
t: usize
}
// blah-dll/lib.rs
#[no_mangle] #[repr(C)] pub struct MyEnumMyTypeWrapper {
pub object: MyEnum<MyType>
}
#[no_mangle] #[repr(C)] pub extern "C" fn my_enum_c(value: usize) -> MyEnumMyTypeWrapper {
MyEnumMyTypeWrapper { object: MyEnum::C(value) }
}
Now I have a DLL file plus a generated wrapper with all the public function pointer definitions
and I want to call my_enum_c
on that generated DLL:
// auto-generated bindings to blah.dll
struct BlahDll {
lib: Box<Library>,
// compiler wants to know where MyEnumMyTypeWrapper is defined
my_enum_c: Symbol<unsafe extern fn(usize) -> MyEnumMyTypeWrapper>,
}
fn main() {
let lib = load_library("blah-dll.so")?;
let value = lib.my_enum_c(5);
}
This doesn't compile because the compiler wants to know the struct definition of MyEnumMyType
.
My question is this: The Rust layout isn't stable, but if I generate the exact same layout of MyEnumMyType
again with a #[repr(C)]
attribute, it should be stable, shouldn't it?
#[repr(C)]
pub struct MyEnumMyTypeWrapperA {
pub object: MyEnum<MyType>
}
and:
enum MyEnumMyType { A, B, C(MyType) } // no repr(C)
#[repr(C)]
pub struct MyEnumMyTypeWrapperB {
pub object: MyEnumMyType
}
Is the binary layout of MyEnumMyTypeWrapperA
equal to the binary layout of MyEnumMyTypeWrapperB
? Thanks in advance for any help.