41 lines
782 B
Rust
Executable File
41 lines
782 B
Rust
Executable File
use alloc::boxed::Box;
|
|
use alloc::vec::Vec;
|
|
use core::mem;
|
|
|
|
#[repr(C)]
|
|
pub struct FFIArray<T> {
|
|
ptr: *mut T,
|
|
len: usize,
|
|
}
|
|
|
|
impl<T> FFIArray<T> {
|
|
pub fn new(v: &[T]) -> Self {
|
|
let boxed_slice: Box<&[T]> = Box::new(v);
|
|
let r = Self {
|
|
ptr: boxed_slice.as_ptr() as *mut T,
|
|
len: boxed_slice.len(),
|
|
};
|
|
mem::forget(boxed_slice);
|
|
r
|
|
}
|
|
|
|
fn delete(&self) {
|
|
unsafe {
|
|
drop(Vec::from_raw_parts(self.ptr, self.len, self.len));
|
|
}
|
|
}
|
|
|
|
pub(crate) fn ptr(&self) -> *mut T {
|
|
self.ptr
|
|
}
|
|
pub(crate) fn len(&self) -> usize {
|
|
self.len
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
#[cfg(not(feature = "mock_data"))]
|
|
extern "wasm" fn delete_ffi_array(a: FFIArray<u8>) {
|
|
a.delete()
|
|
}
|