Skip to main content

AllocationController

Trait AllocationController 

Source
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>> { ... }
}
Available on crate feature serde only.
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§

Source

fn alloc_align(&self) -> usize

The alignment this allocation was created with.

Source

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.

Source

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

Returns a view of the memory of the whole allocation

Provided Methods§

Source

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).

Source

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.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§