Skip to main content

FfiBuffer

Struct FfiBuffer 

Source
#[repr(C)]
pub struct FfiBuffer { pub data: *mut u8, pub len: usize, pub capacity: usize, }
Expand description

FFI-safe byte buffer with explicit ownership semantics.

data points to a heap allocation of capacity bytes managed by Rust’s global allocator. len is the number of initialized bytes.

§Safety

  • This type does not implement Drop. Callers must call ffi_buffer_free to release the memory.
  • Do not copy this struct without transferring ownership—double-free will result.

Fields§

§data: *mut u8§len: usize§capacity: usize

Implementations§

Source§

impl FfiBuffer

Source

pub fn null() -> Self

Returns an FfiBuffer with all fields zero (null data pointer).

Represents an empty / absent buffer. Safe to pass to ffi_buffer_free.

Source

pub fn new(capacity: usize) -> Self

Allocate a new buffer of capacity bytes.

Returns FfiBuffer::null if capacity == 0.

§Panics

Panics if the allocator returns a null pointer (OOM).

Source

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

Consume a Vec<u8> and wrap it as an FfiBuffer.

std::mem::forget is used to prevent Vec from running its destructor; the caller must call ffi_buffer_free when done.

Source

pub fn from_json<T: Serialize>(value: &T) -> Result<Self, FfiError>

Serialize value to JSON and store it in a new FfiBuffer.

Source

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

Return the initialized bytes as a slice.

§Safety

The caller must ensure self.data is valid for self.len bytes and that no concurrent write is occurring.

Source

pub unsafe fn to_json<T: DeserializeOwned>(&self) -> Result<T, FfiError>

Deserialize the buffer’s contents as JSON into type T.

§Safety

Same requirements as as_slice.

Source

pub unsafe fn dealloc(self)

Deallocate the buffer.

§Safety

Must only be called once. After this call self.data is dangling.

Trait Implementations§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.