Trait AllocationController

pub trait AllocationController {
    // Required methods
    fn alloc_align(&self) -> usize;
    unsafe fn memory_mut(&mut self) -> &mut [MaybeUninit<u8>];
    fn memory(&self) -> &[MaybeUninit<u8>];

    // Provided methods
    fn grow(&mut self, size: usize, align: usize) -> Result<(), AllocationError> { ... }
    fn try_detach(&mut self) -> Option<NonNull<u8>> { ... }
}
Expand description

Defines how an [Allocation] can be controlled.

This trait enables type erasure of the allocator after an [Allocation] is created, while still providing methods to modify or manage an existing [Allocation].

Required Methods§

fn alloc_align(&self) -> usize

The alignment this allocation was created with.

unsafe fn memory_mut(&mut self) -> &mut [MaybeUninit<u8>]

Returns a mutable view of the memory of the whole allocation

§Safety

Must only write initialized data to the buffer.

fn memory(&self) -> &[MaybeUninit<u8>]

Returns a view of the memory of the whole allocation

Provided Methods§

fn grow(&mut self, size: usize, align: usize) -> Result<(), AllocationError>

Extends the provided [Allocation] to a new size with specified alignment.

§Errors

Returns an AllocationError if the extension fails (e.g., due to insufficient memory or unsupported operation by the allocator).

fn try_detach(&mut self) -> Option<NonNull<u8>>

Indicates whether the allocation uses the Rust alloc crate and can be safely managed by another data structure.

If true, the allocation is not managed by a memory pool and can be safely deallocated using the alloc crate.

§Notes

This allows the allocation’s pointer to be converted into a native Rust Vec without requiring a new allocation.

Implementing this incorrectly is unsafe and may lead to undefined behavior.

Implementors§