Skip to main content

Buffer

Trait Buffer 

Source
pub trait Buffer {
    // Required methods
    fn allocate(len: usize) -> Self;
    fn as_ptr(this: &Self) -> *const u8;
    unsafe fn as_mut_slice(this: &mut Self, len: usize) -> &mut [u8] ;
    fn into_ptr(this: Self) -> NonNull<u8>;
    unsafe fn from_ptr(ptr: NonNull<u8>, bytes: usize) -> Self;
}
Expand description

A trait wrapping memory management.

This is intended to allow you to bring your own allocator for OwnedBinaryPayloads.

For an implementation example, see the implementation of this trait for Vec.

Required Methods§

Source

fn allocate(len: usize) -> Self

Allocates a buffer of len elements, filled with the default.

Source

fn as_ptr(this: &Self) -> *const u8

Source

unsafe fn as_mut_slice(this: &mut Self, len: usize) -> &mut [u8]

Returns a slice view of size elements of the given memory.

§Safety

The caller must not request more elements than are allocated.

Source

fn into_ptr(this: Self) -> NonNull<u8>

Consumes this ownership and returns a pointer to the start of the arena.

Source

unsafe fn from_ptr(ptr: NonNull<u8>, bytes: usize) -> Self

“Adopts” the memory at the given pointer, taking it under management.

Running the operation:

let owner = OwnerType::allocate(bytes);
let ptr = OwnerType::into_ptr(owner);
let owner = unsafe { OwnerType::from_ptr(ptr, bytes) };

must be a no-op.

§Safety

The pointer must be valid, and the caller must provide the exact size of the given arena.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Buffer for Vec<u8>

Source§

fn allocate(bytes: usize) -> Self

Source§

fn as_ptr(this: &Self) -> *const u8

Source§

unsafe fn as_mut_slice(this: &mut Self, bytes: usize) -> &mut [u8]

Source§

fn into_ptr(this: Self) -> NonNull<u8>

Source§

unsafe fn from_ptr(ptr: NonNull<u8>, bytes: usize) -> Self

Implementors§