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>?
}
slice::from_raw_parts will let you turn those two parameters into a slice reference &[c_uchar], which can then be converted to a Vec<c_uchar> via into() (courtesy of the corresponding From implementation for Vec)
use ::safer_ffi::prelude::*;
#[ffi_export]
pub fn rust_bytes (v: repr_c::Vec<u8>) {
let bytes: Vec<u8> = v.into();
}
Note: in practice, depending on your use-case (you mention "arrays"), the FFI side may not be able to provide a Vec exactly, but just some general "array" of bytes, i.e., [a reference (view) to a] slice of bytes in Rust parlance. That would lead to this other function signature:
fn if_no_ffi (bytes: &'_ [u8])
{
// use bytes; if ownership is required, then:
let owned_bytes = bytes.to_vec();
…
}
Which can be ffi_export-ed quite easily:
use ::safer_ffi::prelude::*;
#[ffi_export]
fn rust_bytes (bytes: c_slice::Ref<'_, u8>)
{
let owned_bytes: Vec<u8> = bytes.to_vec();
…
}