Function vibha::ffi_recover_out_buffer [] [src]

pub unsafe fn ffi_recover_out_buffer<'a>(
    ptr_ref_mut: &'a mut *mut u8,
    len: usize
) -> Cursor<&'a mut [u8]>

Converts the pointer and len for the output buffer into a rusty form.

This accepts a mutable reference to a mut pointer so that the lifetime of the Cursor will be correct. It can't accidentally outlive the pointer it was based on. You also can't accidentally make two buffers to the same block of data.

extern crate vibha;
use vibha::ffi_recover_out_buffer;
use std::io::Write;

const BUFFER_SIZE: usize = 20;
let mut vec: Vec<u8> = Vec::with_capacity(BUFFER_SIZE);
unsafe { vec.set_len(BUFFER_SIZE) };
{
  let len = vec.len();
  let ptr_ref_mut = &mut unsafe { vec.as_mut_ptr() };
  let mut cursor = unsafe { ffi_recover_out_buffer(ptr_ref_mut, len) };
  write!(cursor, "a");
}
assert_eq!(vec[0], 'a' as u8);