Expand description
Utilities for transferring heap-allocated Rust Vec<T>
values across an FFI boundary.
The primary abstraction offered by this module is CVec
, a C-compatible struct that stores
a raw pointer (ptr
) together with the vector’s logical len
and cap
. By moving the
allocation metadata into a plain repr(C)
type we allow the memory created by Rust to be
owned, inspected, and ultimately freed by foreign code (or vice-versa) without introducing
undefined behaviour.
Only a very small API surface is exposed to C:
cvec_new
– create an emptyCVec
sentinel that can be returned to foreign code.
De-allocation is intentionally not provided via a generic helper. Instead each FFI module
must expose its own type-specific vec_*_drop
function which reconstructs the original
Vec<T>
with Vec::from_raw_parts
and allows it to drop. This avoids the size-mismatch risk
that a one-size-fits-all cvec_drop
had in the past.
All other manipulation happens on the Rust side before relinquishing ownership. This keeps the
rules for memory safety straightforward: foreign callers must treat the memory region pointed
to by ptr
as opaque and interact with it solely through the functions provided here.
Structs§
- CVec
CVec
is a C compatible struct that stores an opaque pointer to a block of memory, it’s length and the capacity of the vector it was allocated from.