Trait Allocator

Source
pub unsafe trait Allocator:
    Default
    + Clone
    + Debug
    + Send
    + Sync {
    type TAllocatedMemory: AllocatedMemory;

    // Required methods
    fn allocate(
        &self,
        layout: Layout,
    ) -> Result<Self::TAllocatedMemory, AllocationError>;
    unsafe fn convert_raw_ptr(&self, ptr: *mut u8) -> Self::TAllocatedMemory;
    unsafe fn dangling<T>(&self) -> Self::TAllocatedMemory;
}
Expand description

Represents a memory allocator.

§Safety

This trait is inherently unsafe, because it deals with raw pointers and memory management.

Required Associated Types§

Required Methods§

Source

fn allocate( &self, layout: Layout, ) -> Result<Self::TAllocatedMemory, AllocationError>

Allocates a new AllocatedMemory with the given layout.

§Errors

Returns an AllocationError if the memory cannot be allocated.

Source

unsafe fn convert_raw_ptr(&self, ptr: *mut u8) -> Self::TAllocatedMemory

Creates an AllocatedMemory from a raw pointer.

§Safety

The pointer must originally arise from calling Allocator::allocate. Otherwise the behaviour is undefined.

Source

unsafe fn dangling<T>(&self) -> Self::TAllocatedMemory

Creates a new dangling AllocatedMemory. This pointer is non-zero, not valid but well-aligned. Note that it should not be deallocated, nor dereferenced. It does however represent a valid pointer to the type T.

It is useful for example for creating slices of length 0 and other lazily-initialized things.

§Safety

This function is unsafe, because the pointer is not valid. It is up to the caller to ensure that it is not used directly, in particular it should never be dereferenced and deallocated.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§