我需要一个Rust中的extern "C" FFI函数,并希望接受固定大小的数组。C代码传递的内容类似于:
// C code
extern int(*)[4] call_rust_funct(unsigned char (*)[3]);
....
unsigned char a[] = { 11, 255, 212 };
int(*p)[4] = call_rust_funct(&a);我如何为它编写我的Rust函数?
// 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]))
}发布于 2016-08-29 23:03:21
对于固定大小的数组,您需要使用Rust的语法:
pub unsafe extern "C" fn call_rust_funct(_p: *mut [u8; 3]) -> *mut [i32; 4] {
Box::into_raw(Box::new([99i32; 4]))
}您还可以始终使用*mut std::os::raw::c_void并将其转换为正确的类型。
https://stackoverflow.com/questions/39208831
复制相似问题