BumpAllocatorExt

Trait BumpAllocatorExt 

Source
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 BumpAllocators.

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 BumpAllocators 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§

Source

type Stats<'b>: Into<AnyStats<'b>> where Self: 'b

The type returned by the stats method.

Required Methods§

Source

fn stats(&self) -> Self::Stats<'_>

Returns a type which provides statistics about the memory usage of the bump allocator.

Source

fn allocate_layout(&self, layout: Layout) -> NonNull<u8>

A specialized version of allocate.

Behaves like the following code:

self.allocate(layout).unwrap().cast()
§Panics

Panics if the allocation fails.

Source

fn try_allocate_layout(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>

A specialized version of allocate.

Behaves like the following code:

Ok(self.allocate(layout)?.cast())
§Errors

Errors if the allocation fails.

Source

fn allocate_sized<T>(&self) -> NonNull<T>

A specialized version of allocate.

Behaves like the following code:

self.allocate(Layout::new::<T>()).unwrap().cast()
§Panics

Panics if the allocation fails.

Source

fn try_allocate_sized<T>(&self) -> Result<NonNull<T>, AllocError>

A specialized version of allocate.

Behaves like the following code:

Ok(self.allocate(Layout::new::<T>())?.cast())
§Errors

Errors if the allocation fails.

Source

fn allocate_slice<T>(&self, len: usize) -> NonNull<T>

A specialized version of allocate.

Behaves like the following code:

self.allocate(Layout::array::<T>(len).unwrap()).unwrap().cast()
§Panics

Panics if the allocation fails.

Source

fn try_allocate_slice<T>(&self, len: usize) -> Result<NonNull<T>, AllocError>

A specialized version of allocate.

Behaves like the following code:

Ok(self.allocate(Layout::array::<T>(len).map_err(|_| AllocError)?)?.cast())
§Errors

Errors if the allocation fails.

Source

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.

Source

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.

Source

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.

Source

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 to cap
Source

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 to cap

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.

Implementations on Foreign Types§

Source§

impl<B: BumpAllocatorExt + ?Sized> BumpAllocatorExt for &B

Source§

type Stats<'b> = <B as BumpAllocatorExt>::Stats<'b> where Self: 'b

Source§

fn stats(&self) -> Self::Stats<'_>

Source§

fn allocate_layout(&self, layout: Layout) -> NonNull<u8>

Source§

fn try_allocate_layout(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>

Source§

fn allocate_sized<T>(&self) -> NonNull<T>

Source§

fn try_allocate_sized<T>(&self) -> Result<NonNull<T>, AllocError>

Source§

fn allocate_slice<T>(&self, len: usize) -> NonNull<T>

Source§

fn try_allocate_slice<T>(&self, len: usize) -> Result<NonNull<T>, AllocError>

Source§

unsafe fn shrink_slice<T>( &self, ptr: NonNull<T>, old_len: usize, new_len: usize, ) -> Option<NonNull<T>>

Source§

fn prepare_slice_allocation<T>(&self, len: usize) -> NonNull<[T]>

Source§

fn try_prepare_slice_allocation<T>( &self, len: usize, ) -> Result<NonNull<[T]>, AllocError>

Source§

unsafe fn allocate_prepared_slice<T>( &self, ptr: NonNull<T>, len: usize, cap: usize, ) -> NonNull<[T]>

Source§

unsafe fn allocate_prepared_slice_rev<T>( &self, ptr: NonNull<T>, len: usize, cap: usize, ) -> NonNull<[T]>

Source§

impl<B: BumpAllocatorExt + ?Sized> BumpAllocatorExt for &mut B

Source§

type Stats<'b> = <B as BumpAllocatorExt>::Stats<'b> where Self: 'b

Source§

fn stats(&self) -> Self::Stats<'_>

Source§

fn allocate_layout(&self, layout: Layout) -> NonNull<u8>

Source§

fn try_allocate_layout(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>

Source§

fn allocate_sized<T>(&self) -> NonNull<T>

Source§

fn try_allocate_sized<T>(&self) -> Result<NonNull<T>, AllocError>

Source§

fn allocate_slice<T>(&self, len: usize) -> NonNull<T>

Source§

fn try_allocate_slice<T>(&self, len: usize) -> Result<NonNull<T>, AllocError>

Source§

unsafe fn shrink_slice<T>( &self, ptr: NonNull<T>, old_len: usize, new_len: usize, ) -> Option<NonNull<T>>

Source§

fn prepare_slice_allocation<T>(&self, len: usize) -> NonNull<[T]>

Source§

fn try_prepare_slice_allocation<T>( &self, len: usize, ) -> Result<NonNull<[T]>, AllocError>

Source§

unsafe fn allocate_prepared_slice<T>( &self, ptr: NonNull<T>, len: usize, cap: usize, ) -> NonNull<[T]>

Source§

unsafe fn allocate_prepared_slice_rev<T>( &self, ptr: NonNull<T>, len: usize, cap: usize, ) -> NonNull<[T]>

Implementors§

Source§

impl BumpAllocatorExt for dyn BumpAllocator + '_

Source§

type Stats<'b> = AnyStats<'b> where Self: 'b

Source§

impl BumpAllocatorExt for dyn BumpAllocatorScope<'_> + '_

Source§

type Stats<'b> = AnyStats<'b> where Self: 'b

Source§

impl BumpAllocatorExt for dyn MutBumpAllocator + '_

Source§

type Stats<'b> = AnyStats<'b> where Self: 'b

Source§

impl BumpAllocatorExt for dyn MutBumpAllocatorScope<'_> + '_

Source§

type Stats<'b> = AnyStats<'b> where Self: 'b

Source§

impl<A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, const DEALLOCATES: bool> BumpAllocatorExt for Bump<A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED, DEALLOCATES>
where MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment, A: BaseAllocator<GUARANTEED_ALLOCATED>,

Source§

type Stats<'b> = Stats<'b, A, UP, GUARANTEED_ALLOCATED> where Self: 'b

Source§

impl<A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, const DEALLOCATES: bool> BumpAllocatorExt for BumpScope<'_, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED, DEALLOCATES>
where MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment, A: BaseAllocator<GUARANTEED_ALLOCATED>,

Source§

type Stats<'b> = Stats<'b, A, UP, GUARANTEED_ALLOCATED> where Self: 'b

Source§

impl<B: BumpAllocatorExt> BumpAllocatorExt for WithoutDealloc<B>

Source§

type Stats<'b> = <B as BumpAllocatorExt>::Stats<'b> where Self: 'b

Source§

impl<B: BumpAllocatorExt> BumpAllocatorExt for WithoutShrink<B>

Source§

type Stats<'b> = <B as BumpAllocatorExt>::Stats<'b> where Self: 'b