I need an extern "C"
FFI function in Rust and want to accept an array of fixed size. The C code passes something like:
// C code
extern int(*)[4] call_rust_funct(unsigned char (*)[3]);
....
unsigned char a[] = { 11, 255, 212 };
int(*p)[4] = call_rust_funct(&a);
How do I write my Rust function for it ?
// Pseudo code - DOESN'T COMPILE
pub unsafe extern "C" fn call_rust_funct(_p: *mut u8[3]) -> *mut i32[4] {
Box::into_raw(Box::new([99i32; 4]))
}
You need to use Rust's syntax for fixed size arrays:
pub unsafe extern "C" fn call_rust_funct(_p: *mut [u8; 3]) -> *mut [i32; 4] {
Box::into_raw(Box::new([99i32; 4]))
}
You can also always use *mut std::os::raw::c_void
and transmute it to the correct type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With