#[no_mangle]
pub extern fn rust_bytes(s: *const /* What type should be filled here? */) {
let bytes: Vec<u8> = /// How to write?
}
I collected some information:
https://users.rust-lang.org/t/how-to-return-a-vector-from-c-back-to-rust-ffi/36569/7
https://users.rust-lang.org/t/ffi-how-to-copy-a-vec-u8-into-a-mut-u8/50560
Since the demonstration is incomplete, I still don’t understand.
I tried the code based on the information.
#[no_mangle]
pub extern fn rust_bytes(data: *const c_uchar, data_length: u32) {
let bytes: Vec<u8> = /// How to read data here and convert it into Vec<u8>?
}
I found the way to pass String:
#[no_mangle]
pub extern fn rust_greeting(to: *const c_char) -> *mut c_char {
let c_str = unsafe { CStr::from_ptr(to) };
let recipient = match c_str.to_str() {
Err(_) => "there",
Ok(string) => string,
};
CString::new("Hello ".to_owned() + recipient).unwrap().into_raw()
}
#[no_mangle]
pub extern fn rust_cstr_free(s: *mut c_char) {
unsafe {
if s.is_null() { return }
CString::from_raw(s)
};
}
Since FFI can receive strings, it should also be able to receive arrays. But I checked the relevant information, but did not find the relevant API.
Below is the Rust FFI example I collected:
https://github.com/alexcrichton/rust-ffi-examples/
https://github.com/brickpop/flutter-rust-ffi
If there are more examples of FFI, I hope you can tell me, thank you.