Skip to main content

CdynBox

Struct CdynBox 

Source
#[repr(C)]
pub struct CdynBox { pub data: *mut c_void, pub len: usize, pub free: unsafe extern "C" fn(data: *mut c_void, len: usize), }
Expand description

A cross-module data box: memory allocated and freed by the same module.

This is the cdyn mode’s raw-data smart pointer, complementing the ref-counted instance handle CdynHandle<T>:

  • Instance objects (with vtables) → ref-counted, use AbiStableDynRef’s retain/release via CdynHandle<T>.
  • Raw data buffers (payloads, serialized blobs, arrays) → single-owner, use this struct’s free function pointer.

Both share the same principle: the deallocator lives in the module that allocated the memory, so allocator mismatches across module/CRT boundaries (notably on Windows) can never corrupt the heap.

The box is move-only: there is exactly one owner, and dropping it (or explicitly calling free) hands the memory back to its origin module. Ref-counted sharing of data should be modeled as an instance instead.

§C/C++/Zig side

Any language can produce a box: allocate, fill, and export struct { void* data; size_t len; void (*free)(void*, size_t); } where free calls the module’s own allocator (C++: operator delete[] / std::free, Zig: allocator.free). The layout is #[repr(C)] / plain C struct.

Fields§

§data: *mut c_void

Pointer to the data. Allocated by the producing module.

§len: usize

Length in bytes.

§free: unsafe extern "C" fn(data: *mut c_void, len: usize)

Frees data. Must be a function from the module that allocated it.

Implementations§

Source§

impl CdynBox

Source

pub const fn null() -> Self

A null box (no data, free is a no-op).

Source

pub fn is_null(&self) -> bool

Source

pub fn from_vec(v: Vec<u8>) -> Self

Plugin side (Rust) — wrap an owned Vec<u8> into a box.

The buffer is exact-fit (into_boxed_slice), so len == capacity and the paired cdyn_box_free_rust can reconstruct and free it. The returned free pointer executes in the plugin’s code — the module that owns the allocation.

Source

pub unsafe fn as_slice(&self) -> &[u8]

View the contents as a byte slice.

§Safety

data must point to len readable bytes for the lifetime of &self.

Source

pub fn into_raw(self) -> (Self, bool)

Hand ownership back to the caller (no free on drop afterwards).

Trait Implementations§

Source§

impl Clone for CdynBox

Source§

fn clone(&self) -> CdynBox

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for CdynBox

Source§

impl Debug for CdynBox

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Send for CdynBox

Source§

impl Sync for CdynBox

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.