How to expose a Rust `Vec` to FFI?

后端 未结 2 1333
不知归路
不知归路 2020-12-15 18:21

I\'m trying to construct a pair of elements:

  • array: *mut T
  • array_len: usize

array is intended to

2条回答
  •  [愿得一人]
    2020-12-15 18:54

    You could use [T]::as_mut_ptr to obtain the *mut T pointer directly from Vec, Box<[T]> or any other DerefMut-to-slice types.

    use std::mem;
    
    let mut boxed_slice: Box<[T]> = vector.into_boxed_slice();
    
    let array: *mut T = boxed_slice.as_mut_ptr();
    let array_len: usize = boxed_slice.len();
    
    // Prevent the slice from being destroyed (Leak the memory).
    mem::forget(boxed_slice);
    

提交回复
热议问题