Given a piece of unsafe code (bindings to a system library) of type:
pub fn unsafe_system_call(buf: *mut ::std::os::raw::c_short) {}
I am wrapping it with a function:
pub fn wrapper(buf: &mut [u8]) {
unsafe {
ffi::unsafe_system_call(buf.as_ptr() as *mut _);
}
}
Calling wrapper() with a static array buffer is possible:
let mut buf: [u8; 7] = [0, 0, 0, 0, 0, 0, 0];
wrapper(&mut buf);
But when the size of the buffer should be dynamic, and attempting to use a Vec, it's unclear how to init the buffer:
let mut buf = Vec::with_capacity(runtime_size);
wrapper(&mut buf);
This code just creates a Vec but doesn't initialize the memory in preparation of an unsafe call, and the library call doesn't execute correctly.
What's the right way to do this?