pub unsafe trait BumpAllocatorExt: BumpAllocator {
type Stats<'b>: Into<AnyStats<'b>>
where Self: 'b;
// Required methods
fn stats(&self) -> Self::Stats<'_>;
fn allocate_layout(&self, layout: Layout) -> NonNull<u8>;
fn try_allocate_layout(
&self,
layout: Layout,
) -> Result<NonNull<u8>, AllocError>;
fn allocate_sized<T>(&self) -> NonNull<T>;
fn try_allocate_sized<T>(&self) -> Result<NonNull<T>, AllocError>;
fn allocate_slice<T>(&self, len: usize) -> NonNull<T>;
fn try_allocate_slice<T>(
&self,
len: usize,
) -> Result<NonNull<T>, AllocError>;
unsafe fn shrink_slice<T>(
&self,
ptr: NonNull<T>,
old_len: usize,
new_len: usize,
) -> Option<NonNull<T>>;
fn prepare_slice_allocation<T>(&self, cap: usize) -> NonNull<[T]>;
fn try_prepare_slice_allocation<T>(
&self,
cap: usize,
) -> Result<NonNull<[T]>, AllocError>;
unsafe fn allocate_prepared_slice<T>(
&self,
ptr: NonNull<T>,
len: usize,
cap: usize,
) -> NonNull<[T]>;
unsafe fn allocate_prepared_slice_rev<T>(
&self,
ptr: NonNull<T>,
len: usize,
cap: usize,
) -> NonNull<[T]>;
}
Expand description
An extension trait for BumpAllocator
s.
Its main purpose is to provide methods that are optimized for a certain T
and error behavior.
It also provides stats
to get a Bump
specific Stats
object.
Note: This trait is not automatically implemented for all BumpAllocator
s
because it is meant to provide specialized methods and types for better performance.
A blanket implementation for all BumpAllocators
would defeat that purpose, at least
until some form of specialization is stabilized.
Required Associated Types§
Required Methods§
Sourcefn stats(&self) -> Self::Stats<'_>
fn stats(&self) -> Self::Stats<'_>
Returns a type which provides statistics about the memory usage of the bump allocator.
Sourcefn allocate_layout(&self, layout: Layout) -> NonNull<u8>
fn allocate_layout(&self, layout: Layout) -> NonNull<u8>
Sourcefn try_allocate_layout(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>
fn try_allocate_layout(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>
Sourcefn allocate_sized<T>(&self) -> NonNull<T>
fn allocate_sized<T>(&self) -> NonNull<T>
Sourcefn try_allocate_sized<T>(&self) -> Result<NonNull<T>, AllocError>
fn try_allocate_sized<T>(&self) -> Result<NonNull<T>, AllocError>
Sourcefn allocate_slice<T>(&self, len: usize) -> NonNull<T>
fn allocate_slice<T>(&self, len: usize) -> NonNull<T>
Sourcefn try_allocate_slice<T>(&self, len: usize) -> Result<NonNull<T>, AllocError>
fn try_allocate_slice<T>(&self, len: usize) -> Result<NonNull<T>, AllocError>
Sourceunsafe fn shrink_slice<T>(
&self,
ptr: NonNull<T>,
old_len: usize,
new_len: usize,
) -> Option<NonNull<T>>
unsafe fn shrink_slice<T>( &self, ptr: NonNull<T>, old_len: usize, new_len: usize, ) -> Option<NonNull<T>>
A specialized version of shrink
.
Behaves like the following code except that it returns None
when the allocation remains unchanged and the pointer stays valid.
self.shrink(ptr.cast(),
Layout::array::<T>(old_len).unwrap_unchecked(),
Layout::array::<T>(new_len).unwrap_unchecked(),
).unwrap_unchecked().cast()
§Safety
Same safety conditions as for the code above apply.
Sourcefn prepare_slice_allocation<T>(&self, cap: usize) -> NonNull<[T]>
fn prepare_slice_allocation<T>(&self, cap: usize) -> NonNull<[T]>
A specialized version of prepare_allocation
.
Returns a [T]
of free space in the bump allocator.
§Panics
Panics if the allocation fails.
Sourcefn try_prepare_slice_allocation<T>(
&self,
cap: usize,
) -> Result<NonNull<[T]>, AllocError>
fn try_prepare_slice_allocation<T>( &self, cap: usize, ) -> Result<NonNull<[T]>, AllocError>
A specialized version of prepare_allocation
.
Returns a [T]
of free space in the bump allocator.
§Errors
Errors if the allocation fails.
Sourceunsafe fn allocate_prepared_slice<T>(
&self,
ptr: NonNull<T>,
len: usize,
cap: usize,
) -> NonNull<[T]>
unsafe fn allocate_prepared_slice<T>( &self, ptr: NonNull<T>, len: usize, cap: usize, ) -> NonNull<[T]>
A specialized version of allocate_prepared
.
Allocates part of the free space returned from a
(try_)prepare_slice_allocation
call.
§Safety
ptr..ptr + cap
must be the pointer range returned from(try_)prepare_slice_allocation
.- no allocation, grow, shrink or deallocate must have taken place since then
- no resets must have taken place since then
len
must be less than or equal tocap
Sourceunsafe fn allocate_prepared_slice_rev<T>(
&self,
ptr: NonNull<T>,
len: usize,
cap: usize,
) -> NonNull<[T]>
unsafe fn allocate_prepared_slice_rev<T>( &self, ptr: NonNull<T>, len: usize, cap: usize, ) -> NonNull<[T]>
A specialized version of allocate_prepared_rev
.
Allocates part of the free space returned from a
(try_)prepare_slice_allocation
call.
§Safety
ptr - cap..ptr
must be the pointer range returned from(try_)prepare_slice_allocation
.- no allocation, grow, shrink or deallocate must have taken place since then
- no resets must have taken place since then
len
must be less than or equal tocap
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.