#[repr(C)]pub struct CVec {
pub ptr: *mut c_void,
pub len: usize,
pub cap: usize,
}Expand description
CVec is a C compatible struct that stores an opaque pointer to a block of
memory, its length and the capacity of the vector it was allocated from.
§Safety
Changing the values here may lead to undefined behavior when the memory is dropped.
Fields§
§ptr: *mut c_voidOpaque pointer to block of memory storing elements to access the elements cast it to the underlying type.
len: usizeThe number of elements in the block.
cap: usizeThe capacity of vector from which it was allocated. Used when deallocating the memory
Implementations§
Source§impl CVec
impl CVec
Sourcepub fn empty() -> Self
pub fn empty() -> Self
Returns an empty CVec.
This is primarily useful for constructing a sentinel value that represents the absence of data when crossing the FFI boundary.
Uses a dangling pointer (like Vec::new()) rather than null to satisfy
Vec::from_raw_parts preconditions when the CVec is later dropped.
Sourcepub unsafe fn into_vec<T>(self) -> Vec<T>
pub unsafe fn into_vec<T>(self) -> Vec<T>
Reconstructs and consumes the Rust vector represented by this value.
§Safety
For non-zero capacity, ptr, len, and cap must describe exactly one live allocation
originally created by Vec<T> with len initialized elements. The allocation must not be
accessed or reconstructed again after this call.
§Panics
Panics if len > cap, len != 0 when cap == 0, or a non-empty allocation has a null
pointer.
Trait Implementations§
Source§impl<T> From<Vec<T>> for CVec
Consumes and leaks the Vec, returning a mutable pointer to the contents as
a CVec. The memory has been leaked and now exists for the lifetime of the
program unless dropped manually.
Note: drop the memory by reconstructing the vec using from_raw_parts method
as shown in the test below.
impl<T> From<Vec<T>> for CVec
Consumes and leaks the Vec, returning a mutable pointer to the contents as
a CVec. The memory has been leaked and now exists for the lifetime of the
program unless dropped manually.
Note: drop the memory by reconstructing the vec using from_raw_parts method
as shown in the test below.