pub trait Allocator: Clone {
// Required methods
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>;
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>;
unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>;
fn allocate_zeroed(
&self,
layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>;
}Expand description
Trait for allocators used by Box and other collections.
This trait mirrors the core::alloc::Allocator trait,
returning Result<NonNull<[u8]>, AllocError> to provide the actual
size allocated.
Implementations of this trait must tolerate being passed zero-sized layouts.
For zero-sized allocations, implementations should return a dangling pointer
with the appropriate alignment, and deallocate should be a no-op for such pointers.
Required Methods§
Sourcefn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
Allocates memory as described by the given layout.
If the layout has a size of zero, this method returns a dangling pointer.
Sourceunsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
§Safety
- The pointer must have been allocated by
allocatewith the same layout.
If the layout has a size of zero, this method is a no-op.
Sourceunsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
§Safety
ptrmust denote a block of memory currently allocated via this allocator.old_layoutmust fit that block of memory (Thenew_layoutargument need not fit it.).new_layout.size()must be greater than or equal toold_layout.size().
Note that new_layout.align() need not be the same as old_layout.align().
If the new layout has a size of zero, this method returns a dangling pointer.
Sourceunsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
§Safety
ptrmust denote a block of memory currently allocated via this allocator.old_layoutmust fit that block of memory (Thenew_layoutargument need not fit it.).new_layout.size()must be less than or equal toold_layout.size().
Note that new_layout.align() need not be the same as old_layout.align().
If the new layout has a size of zero, this method deallocates the memory and returns a dangling pointer.
Sourcefn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
Allocates zeroed memory as described by the given layout.
If the layout has a size of zero, this method returns a dangling pointer.
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.