Skip to main content

Bump

Struct Bump 

Source
pub struct Bump<A = Global, S = BumpSettings>{ /* private fields */ }
Expand description

The bump allocator.

§Generic parameters

  • A — the base allocator, defaults to Global when the alloc feature is enabled
  • S — the bump allocator settings, see settings

§Overview

All of the mentioned methods that do allocations panic if the base allocator returned an error. For every such panicking method, there is a corresponding try_-prefixed version that returns a Result instead.

§Create a Bump
§Allocate …
§Free memory using …
§Configure allocator settings …

§Collections

A Bump (and BumpScope) can be used to allocate collections of this crate…

use bump_scope::{Bump, BumpString};
let bump: Bump = Bump::new();

let mut string = BumpString::new_in(&bump);
string.push_str("Hello,");
string.push_str(" world!");

… and collections from crates that use allocator_api2’s Allocator like hashbrown’s HashMap:

This requires the allocator-api2-02 feature OR the nightly-allocator-api feature along with hashbrown’s nightly feature.

use bump_scope::Bump;
use hashbrown::HashMap;

let bump: Bump = Bump::new();
let mut map = HashMap::new_in(&bump);
map.insert("tau", 6.283);

On nightly and with the feature nightly-allocator-api you can also allocate collections from std that have an allocator parameter:

#![feature(allocator_api, btreemap_alloc)]
use bump_scope::Bump;
use std::collections::{VecDeque, BTreeMap, LinkedList};

let bump: Bump = Bump::new();
let vec = Vec::new_in(&bump);
let queue = VecDeque::new_in(&bump);
let map = BTreeMap::new_in(&bump);
let list = LinkedList::new_in(&bump);

§Gotcha

Having live allocations and entering bump scopes at the same time requires a BumpScope. This is due to the way lifetimes work, since Bump returns allocations with the lifetime of its own borrow instead of a separate lifetime like BumpScope does.

So you can’t do this:

let mut bump: Bump = Bump::new();

let one = bump.alloc(1);

bump.scoped(|bump| {
    // whatever
});

But you can make the code work by converting the Bump it to a BumpScope first using as_mut_scope:

let mut bump: Bump = Bump::new();
let bump = bump.as_mut_scope();

let one = bump.alloc(1);

bump.scoped(|bump| {
    // whatever
});

Implementations§

Source§

impl<A, S> Bump<A, S>

Methods for a Bump with a default base allocator.

Source

pub fn new() -> Self

Constructs a new Bump without allocating.

§Examples
use bump_scope::Bump;

let bump: Bump = Bump::new();
Source

pub fn with_size(size: usize) -> Self

Constructs a new Bump with a size hint for the first chunk.

If you want to ensure a specific capacity, use with_capacity instead.

An effort is made to ensure the size requested from the base allocator is friendly to an allocator that uses size classes and stores metadata alongside allocations. To achieve this, the requested size is rounded up to either the next power of two or the next multiple of 0x1000, whichever is smaller. After that, the size of [usize; 2] is subtracted.

If the base allocator returns a memory block that is larger than requested, then the chunk will use the extra space.

Disclaimer: The way in which the chunk layout is calculated might change. Such a change is not considered semver breaking.

§Panics

Panics if the allocation fails.

§Examples
use bump_scope::Bump;

// `Bump` with a roughly 1 Mebibyte sized chunk
let bump_1mib: Bump = Bump::with_size(1024 * 1024);
Source

pub fn try_with_size(size: usize) -> Result<Self, AllocError>

Constructs a new Bump with a size hint for the first chunk.

If you want to ensure a specific capacity, use try_with_capacity instead.

An effort is made to ensure the size requested from the base allocator is friendly to an allocator that uses size classes and stores metadata alongside allocations. To achieve this, the requested size is rounded up to either the next power of two or the next multiple of 0x1000, whichever is smaller. After that, the size of [usize; 2] is subtracted.

If the base allocator returns a memory block that is larger than requested, then the chunk will use the extra space.

Disclaimer: The way in which the chunk layout is calculated might change. Such a change is not considered semver breaking.

§Errors

Errors if the allocation fails.

§Examples
use bump_scope::Bump;

// `Bump` with a roughly 1 Mebibyte sized chunk
let bump_1mib: Bump = Bump::try_with_size(1024 * 1024)?;
Source

pub fn with_capacity(layout: Layout) -> Self

Constructs a new Bump with at least enough space for layout.

To construct a Bump with a size hint use with_size instead.

§Panics

Panics if the allocation fails.

§Examples
use bump_scope::Bump;
use core::alloc::Layout;

let layout = Layout::array::<u8>(1234).unwrap();
let bump: Bump = Bump::with_capacity(layout);
assert!(bump.stats().capacity() >= layout.size());
Source

pub fn try_with_capacity(layout: Layout) -> Result<Self, AllocError>

Constructs a new Bump with at least enough space for layout.

To construct a Bump with a size hint use try_with_size instead.

§Errors

Errors if the allocation fails.

§Examples
use bump_scope::Bump;
use core::alloc::Layout;

let layout = Layout::array::<u8>(1234).unwrap();
let bump: Bump = Bump::try_with_capacity(layout)?;
assert!(bump.stats().capacity() >= layout.size());
Source§

impl<A, S> Bump<A, S>

Methods that are always available.

Source

pub const fn new_in(allocator: A) -> Self

Constructs a new Bump without allocating.

§Examples
use bump_scope::Bump;
use bump_scope::alloc::Global;

let bump: Bump = Bump::new_in(Global);
Source

pub fn with_size_in(size: usize, allocator: A) -> Self

Constructs a new Bump with a size hint for the first chunk.

If you want to ensure a specific capacity, use with_capacity_in instead.

An effort is made to ensure the size requested from the base allocator is friendly to an allocator that uses size classes and stores metadata alongside allocations. To achieve this, the requested size is rounded up to either the next power of two or the next multiple of 0x1000, whichever is smaller. After that, the size of [usize; 2] is subtracted.

If the base allocator returns a memory block that is larger than requested, then the chunk will use the extra space.

Disclaimer: The way in which the chunk layout is calculated might change. Such a change is not considered semver breaking.

§Panics

Panics if the allocation fails.

§Examples
use bump_scope::Bump;
use bump_scope::alloc::Global;

// `Bump` with a roughly 1 Mebibyte sized chunk
let bump_1mib: Bump = Bump::with_size_in(1024 * 1024, Global);
Source

pub fn try_with_size_in(size: usize, allocator: A) -> Result<Self, AllocError>

Constructs a new Bump with a size hint for the first chunk.

If you want to ensure a specific capacity, use try_with_capacity instead.

An effort is made to ensure the size requested from the base allocator is friendly to an allocator that uses size classes and stores metadata alongside allocations. To achieve this, the requested size is rounded up to either the next power of two or the next multiple of 0x1000, whichever is smaller. After that, the size of [usize; 2] is subtracted.

If the base allocator returns a memory block that is larger than requested, then the chunk will use the extra space.

Disclaimer: The way in which the chunk layout is calculated might change. Such a change is not considered semver breaking.

§Errors

Errors if the allocation fails.

§Examples
use bump_scope::Bump;
use bump_scope::alloc::Global;

// `Bump` with a roughly 1 Mebibyte sized chunk
let bump_1mib: Bump = Bump::try_with_size_in(1024 * 1024, Global)?;
Source

pub fn with_capacity_in(layout: Layout, allocator: A) -> Self

Constructs a new Bump with at least enough space for layout.

To construct a Bump with a size hint use with_size_in instead.

§Panics

Panics if the allocation fails.

§Examples
use bump_scope::Bump;
use bump_scope::alloc::Global;
use core::alloc::Layout;

let layout = Layout::array::<u8>(1234).unwrap();
let bump: Bump = Bump::with_capacity_in(layout, Global);
assert!(bump.stats().capacity() >= layout.size());
Source

pub fn try_with_capacity_in( layout: Layout, allocator: A, ) -> Result<Self, AllocError>

Constructs a new Bump with at least enough space for layout.

To construct a Bump with a size hint use try_with_size_in instead.

§Errors

Errors if the allocation fails.

§Examples
use bump_scope::Bump;
use bump_scope::alloc::Global;
use core::alloc::Layout;

let layout = Layout::array::<u8>(1234).unwrap();
let bump: Bump = Bump::try_with_capacity_in(layout, Global)?;
assert!(bump.stats().capacity() >= layout.size());
Source

pub fn reset(&mut self)

Deallocates every chunk but the newest, which is also the biggest.

use bump_scope::Bump;

let mut bump: Bump = Bump::with_size(512);

// won't fit in the first chunk
bump.alloc_uninit_slice::<u8>(600);

let chunks = bump.stats().small_to_big().collect::<Vec<_>>();
assert_eq!(chunks.len(), 2);
assert!(chunks[0].size() < chunks[1].size());
assert_eq!(chunks[0].allocated(), 0);
assert_eq!(chunks[1].allocated(), 600);
let last_chunk_size = chunks[1].size();

bump.reset();

let chunks = bump.stats().small_to_big().collect::<Vec<_>>();
assert_eq!(chunks.len(), 1);
assert_eq!(chunks[0].size(), last_chunk_size);
assert_eq!(chunks[0].allocated(), 0);
Source

pub fn stats(&self) -> Stats<'_, S>

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

Source

pub fn as_scope(&self) -> &BumpScope<'_, A, S>

Returns this &Bump as a &BumpScope.

Source

pub fn as_mut_scope(&mut self) -> &mut BumpScope<'_, A, S>

Returns this &mut Bump as a &mut BumpScope.

Source

pub fn with_settings<NewS>(self) -> Bump<A, NewS>

Converts this Bump into a Bump with new settings.

Not every setting can be converted to. This function will fail to compile if:

  • NewS::UP != S::UP
  • NewS::GUARANTEED_ALLOCATED > S::GUARANTEED_ALLOCATED
  • NewS::CLAIMABLE < S::CLAIMABLE
Source

pub fn borrow_with_settings<NewS>(&self) -> &Bump<A, NewS>

Borrows this Bump with new settings.

Not every settings can be converted to. This function will fail to compile if:

  • NewS::MIN_ALIGN != S::MIN_ALIGN
  • NewS::UP != S::UP
  • NewS::GUARANTEED_ALLOCATED > S::GUARANTEED_ALLOCATED
  • NewS::CLAIMABLE != S::CLAIMABLE
Source

pub fn borrow_mut_with_settings<NewS>(&mut self) -> &mut Bump<A, NewS>

Borrows this Bump mutably with new settings.

Not every settings can be converted to. This function will fail to compile if:

  • NewS::MIN_ALIGN < S::MIN_ALIGN
  • NewS::UP != S::UP
  • NewS::GUARANTEED_ALLOCATED != S::GUARANTEED_ALLOCATED
  • NewS::CLAIMABLE != S::CLAIMABLE
Source§

impl<S> Bump<Global, S>

Source

pub fn into_raw(self) -> NonNull<()>

Available on crate feature alloc only.

Converts this Bump into a raw pointer.

use bump_scope::Bump;

let bump: Bump = Bump::new();
let ptr = bump.into_raw();
let bump: Bump = unsafe { Bump::from_raw(ptr) };

bump.alloc_str("Why did i do this?");
Source

pub unsafe fn from_raw(ptr: NonNull<()>) -> Self

Available on crate feature alloc only.

Converts the raw pointer that was created with into_raw back into a Bump.

§Safety
  • ptr must have been created with Self::into_raw.
  • This function must only be called once with this ptr.
  • The settings must match the original ones.
Source§

impl<A, S> Bump<A, S>

Methods that forward to traits.

Source

pub fn claim(&self) -> BumpClaimGuard<'_, '_, A, S>

Source

pub fn is_claimed(&self) -> bool

Source

pub fn scope_guard(&mut self) -> BumpScopeGuard<'_, A, S>

Source

pub fn scoped<R>(&mut self, f: impl FnOnce(&mut BumpScope<'_, A, S>) -> R) -> R

Forwards to BumpAllocator::scoped.

Source

pub fn scoped_aligned<const NEW_MIN_ALIGN: usize, R>( &mut self, f: impl FnOnce(&mut BumpScope<'_, A, S::WithMinimumAlignment<NEW_MIN_ALIGN>>) -> R, ) -> R

Source

pub fn aligned<const NEW_MIN_ALIGN: usize, R>( &mut self, f: impl FnOnce(&mut BumpScope<'_, A, S::WithMinimumAlignment<NEW_MIN_ALIGN>>) -> R, ) -> R

Source

pub fn checkpoint(&self) -> Checkpoint

Source

pub unsafe fn reset_to(&self, checkpoint: Checkpoint)

Source

pub fn allocator(&self) -> &A

Source

pub fn alloc<T>(&self, value: T) -> BumpBox<'_, T>

Source

pub fn try_alloc<T>(&self, value: T) -> Result<BumpBox<'_, T>, AllocError>

Source

pub fn alloc_with<T>(&self, f: impl FnOnce() -> T) -> BumpBox<'_, T>

Source

pub fn try_alloc_with<T>( &self, f: impl FnOnce() -> T, ) -> Result<BumpBox<'_, T>, AllocError>

Source

pub fn alloc_default<T: Default>(&self) -> BumpBox<'_, T>

Source

pub fn try_alloc_default<T: Default>( &self, ) -> Result<BumpBox<'_, T>, AllocError>

Source

pub fn alloc_clone<T: CloneToUninit + ?Sized>( &self, value: &T, ) -> BumpBox<'_, T>

Available on crate feature nightly-clone-to-uninit only.
Source

pub fn try_alloc_clone<T: CloneToUninit + ?Sized>( &self, value: &T, ) -> Result<BumpBox<'_, T>, AllocError>

Available on crate feature nightly-clone-to-uninit only.
Source

pub fn alloc_slice_move<T>( &self, slice: impl OwnedSlice<Item = T>, ) -> BumpBox<'_, [T]>

Source

pub fn try_alloc_slice_move<T>( &self, slice: impl OwnedSlice<Item = T>, ) -> Result<BumpBox<'_, [T]>, AllocError>

Source

pub fn alloc_slice_copy<T: Copy>(&self, slice: &[T]) -> BumpBox<'_, [T]>

Source

pub fn try_alloc_slice_copy<T: Copy>( &self, slice: &[T], ) -> Result<BumpBox<'_, [T]>, AllocError>

Source

pub fn alloc_slice_clone<T: Clone>(&self, slice: &[T]) -> BumpBox<'_, [T]>

Source

pub fn try_alloc_slice_clone<T: Clone>( &self, slice: &[T], ) -> Result<BumpBox<'_, [T]>, AllocError>

Source

pub fn alloc_slice_fill<T: Clone>( &self, len: usize, value: T, ) -> BumpBox<'_, [T]>

Source

pub fn try_alloc_slice_fill<T: Clone>( &self, len: usize, value: T, ) -> Result<BumpBox<'_, [T]>, AllocError>

Source

pub fn alloc_slice_fill_with<T>( &self, len: usize, f: impl FnMut() -> T, ) -> BumpBox<'_, [T]>

Source

pub fn try_alloc_slice_fill_with<T>( &self, len: usize, f: impl FnMut() -> T, ) -> Result<BumpBox<'_, [T]>, AllocError>

Source

pub fn alloc_str(&self, src: &str) -> BumpBox<'_, str>

Source

pub fn try_alloc_str(&self, src: &str) -> Result<BumpBox<'_, str>, AllocError>

Source

pub fn alloc_fmt(&self, args: Arguments<'_>) -> BumpBox<'_, str>

Source

pub fn try_alloc_fmt( &self, args: Arguments<'_>, ) -> Result<BumpBox<'_, str>, AllocError>

Source

pub fn alloc_fmt_mut(&mut self, args: Arguments<'_>) -> BumpBox<'_, str>

Source

pub fn try_alloc_fmt_mut( &mut self, args: Arguments<'_>, ) -> Result<BumpBox<'_, str>, AllocError>

Source

pub fn alloc_cstr(&self, src: &CStr) -> &CStr

Source

pub fn try_alloc_cstr(&self, src: &CStr) -> Result<&CStr, AllocError>

Source

pub fn alloc_cstr_from_str(&self, src: &str) -> &CStr

Source

pub fn try_alloc_cstr_from_str(&self, src: &str) -> Result<&CStr, AllocError>

Source

pub fn alloc_cstr_fmt(&self, args: Arguments<'_>) -> &CStr

Source

pub fn try_alloc_cstr_fmt( &self, args: Arguments<'_>, ) -> Result<&CStr, AllocError>

Source

pub fn alloc_cstr_fmt_mut(&mut self, args: Arguments<'_>) -> &CStr

Source

pub fn try_alloc_cstr_fmt_mut( &mut self, args: Arguments<'_>, ) -> Result<&CStr, AllocError>

Source

pub fn alloc_iter<T>( &self, iter: impl IntoIterator<Item = T>, ) -> BumpBox<'_, [T]>

Source

pub fn try_alloc_iter<T>( &self, iter: impl IntoIterator<Item = T>, ) -> Result<BumpBox<'_, [T]>, AllocError>

Source

pub fn alloc_iter_exact<T, I>( &self, iter: impl IntoIterator<Item = T, IntoIter = I>, ) -> BumpBox<'_, [T]>
where I: ExactSizeIterator<Item = T>,

Source

pub fn try_alloc_iter_exact<T, I>( &self, iter: impl IntoIterator<Item = T, IntoIter = I>, ) -> Result<BumpBox<'_, [T]>, AllocError>
where I: ExactSizeIterator<Item = T>,

Source

pub fn alloc_iter_mut<T>( &mut self, iter: impl IntoIterator<Item = T>, ) -> BumpBox<'_, [T]>

Source

pub fn try_alloc_iter_mut<T>( &mut self, iter: impl IntoIterator<Item = T>, ) -> Result<BumpBox<'_, [T]>, AllocError>

Source

pub fn alloc_iter_mut_rev<T>( &mut self, iter: impl IntoIterator<Item = T>, ) -> BumpBox<'_, [T]>

Source

pub fn try_alloc_iter_mut_rev<T>( &mut self, iter: impl IntoIterator<Item = T>, ) -> Result<BumpBox<'_, [T]>, AllocError>

Source

pub fn alloc_uninit<T>(&self) -> BumpBox<'_, MaybeUninit<T>>

Source

pub fn try_alloc_uninit<T>( &self, ) -> Result<BumpBox<'_, MaybeUninit<T>>, AllocError>

Source

pub fn alloc_uninit_slice<T>(&self, len: usize) -> BumpBox<'_, [MaybeUninit<T>]>

Source

pub fn try_alloc_uninit_slice<T>( &self, len: usize, ) -> Result<BumpBox<'_, [MaybeUninit<T>]>, AllocError>

Source

pub fn alloc_uninit_slice_for<T>( &self, slice: &[T], ) -> BumpBox<'_, [MaybeUninit<T>]>

Source

pub fn try_alloc_uninit_slice_for<T>( &self, slice: &[T], ) -> Result<BumpBox<'_, [MaybeUninit<T>]>, AllocError>

Source

pub fn dealloc<T: ?Sized>(&self, boxed: BumpBox<'_, T>)

Source

pub fn reserve(&self, additional: usize)

Source

pub fn try_reserve(&self, additional: usize) -> Result<(), AllocError>

Source§

impl<A, S> Bump<A, S>

Additional alloc methods that are not available in traits.

Source

pub fn alloc_try_with<T, E>( &self, f: impl FnOnce() -> Result<T, E>, ) -> Result<BumpBox<'_, T>, E>

Allocates the result of f in the bump allocator, then moves E out of it and deallocates the space it took up.

This can be more performant than allocating T after the fact, as Result<T, E> may be constructed in the bump allocators memory instead of on the stack and then copied over.

There is also alloc_try_with_mut, optimized for a mutable reference.

§Panics

Panics if the allocation fails.

§Examples
let result = bump.alloc_try_with(|| -> Result<i32, i32> { Ok(123) });
assert_eq!(result.unwrap(), 123);
assert_eq!(bump.stats().allocated(), offset_of!(Result<i32, i32>, Ok.0) + size_of::<i32>());
let result = bump.alloc_try_with(|| -> Result<i32, i32> { Err(123) });
assert_eq!(result.unwrap_err(), 123);
assert_eq!(bump.stats().allocated(), 0);
Source

pub fn try_alloc_try_with<T, E>( &self, f: impl FnOnce() -> Result<T, E>, ) -> Result<Result<BumpBox<'_, T>, E>, AllocError>

Allocates the result of f in the bump allocator, then moves E out of it and deallocates the space it took up.

This can be more performant than allocating T after the fact, as Result<T, E> may be constructed in the bump allocators memory instead of on the stack and then copied over.

There is also try_alloc_try_with_mut, optimized for a mutable reference.

§Errors

Errors if the allocation fails.

§Examples
let result = bump.try_alloc_try_with(|| -> Result<i32, i32> { Ok(123) })?;
assert_eq!(result.unwrap(), 123);
assert_eq!(bump.stats().allocated(), offset_of!(Result<i32, i32>, Ok.0) + size_of::<i32>());
let result = bump.try_alloc_try_with(|| -> Result<i32, i32> { Err(123) })?;
assert_eq!(result.unwrap_err(), 123);
assert_eq!(bump.stats().allocated(), 0);
Source

pub fn alloc_try_with_mut<T, E>( &mut self, f: impl FnOnce() -> Result<T, E>, ) -> Result<BumpBox<'_, T>, E>

Allocates the result of f in the bump allocator, then moves E out of it and deallocates the space it took up.

This can be more performant than allocating T after the fact, as Result<T, E> may be constructed in the bump allocators memory instead of on the stack and then copied over.

This is just like alloc_try_with, but optimized for a mutable reference.

§Panics

Panics if the allocation fails.

§Examples
let result = bump.alloc_try_with_mut(|| -> Result<i32, i32> { Ok(123) });
assert_eq!(result.unwrap(), 123);
assert_eq!(bump.stats().allocated(), offset_of!(Result<i32, i32>, Ok.0) + size_of::<i32>());
let result = bump.alloc_try_with_mut(|| -> Result<i32, i32> { Err(123) });
assert_eq!(result.unwrap_err(), 123);
assert_eq!(bump.stats().allocated(), 0);
Source

pub fn try_alloc_try_with_mut<T, E>( &mut self, f: impl FnOnce() -> Result<T, E>, ) -> Result<Result<BumpBox<'_, T>, E>, AllocError>

Allocates the result of f in the bump allocator, then moves E out of it and deallocates the space it took up.

This can be more performant than allocating T after the fact, as Result<T, E> may be constructed in the bump allocators memory instead of on the stack and then copied over.

This is just like try_alloc_try_with, but optimized for a mutable reference.

§Errors

Errors if the allocation fails.

§Examples
let result = bump.try_alloc_try_with_mut(|| -> Result<i32, i32> { Ok(123) })?;
assert_eq!(result.unwrap(), 123);
assert_eq!(bump.stats().allocated(), offset_of!(Result<i32, i32>, Ok.0) + size_of::<i32>());
let result = bump.try_alloc_try_with_mut(|| -> Result<i32, i32> { Err(123) })?;
assert_eq!(result.unwrap_err(), 123);
assert_eq!(bump.stats().allocated(), 0);

Trait Implementations§

Source§

impl<A, S> Allocator for &mut Bump<A, S>

Available on crate feature allocator-api2-03 only.
Source§

fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, TargetAllocError>

Attempts to allocate a block of memory. Read more
Source§

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)

Deallocates the memory referenced by ptr. Read more
Source§

unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, TargetAllocError>

Attempts to extend the memory block. Read more
Source§

unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, TargetAllocError>

Behaves like grow, but also ensures that the new contents are set to zero before being returned. Read more
Source§

unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, TargetAllocError>

Attempts to shrink the memory block. Read more
Source§

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

Behaves like allocate, but also ensures that the returned memory is zero-initialized. Read more
Source§

fn by_ref(&self) -> &Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Allocator. Read more
Source§

impl<A, S> Allocator for Bump<A, S>

Source§

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

Attempts to allocate a block of memory. Read more
Source§

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)

Deallocates the memory referenced by ptr. Read more
Source§

unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

Attempts to extend the memory block. Read more
Source§

unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

Behaves like grow, but also ensures that the new contents are set to zero before being returned. Read more
Source§

unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

Attempts to shrink the memory block. Read more
Source§

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

Behaves like allocate, but also ensures that the returned memory is zero-initialized. Read more
Source§

fn by_ref(&self) -> &Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Allocator. Read more
Source§

impl<A, S> Allocator for Bump<A, S>

Available on crate feature nightly-allocator-api only.
Source§

fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, TargetAllocError>

🔬This is a nightly-only experimental API. (allocator_api)
Attempts to allocate a block of memory. Read more
Source§

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)

🔬This is a nightly-only experimental API. (allocator_api)
Deallocates the memory referenced by ptr. Read more
Source§

unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, TargetAllocError>

🔬This is a nightly-only experimental API. (allocator_api)
Attempts to extend the memory block. Read more
Source§

unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, TargetAllocError>

🔬This is a nightly-only experimental API. (allocator_api)
Behaves like grow, but also ensures that the new contents are set to zero before being returned. Read more
Source§

unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, TargetAllocError>

🔬This is a nightly-only experimental API. (allocator_api)
Attempts to shrink the memory block. Read more
Source§

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

🔬This is a nightly-only experimental API. (allocator_api)
Behaves like allocate, but also ensures that the returned memory is zero-initialized. Read more
Source§

fn by_ref(&self) -> &Self
where Self: Sized,

🔬This is a nightly-only experimental API. (allocator_api)
Creates a “by reference” adapter for this instance of Allocator. Read more
Source§

impl<A, S> Allocator for Bump<A, S>

Available on crate feature allocator-api2-03 only.
Source§

fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, TargetAllocError>

Attempts to allocate a block of memory. Read more
Source§

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)

Deallocates the memory referenced by ptr. Read more
Source§

unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, TargetAllocError>

Attempts to extend the memory block. Read more
Source§

unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, TargetAllocError>

Behaves like grow, but also ensures that the new contents are set to zero before being returned. Read more
Source§

unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, TargetAllocError>

Attempts to shrink the memory block. Read more
Source§

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

Behaves like allocate, but also ensures that the returned memory is zero-initialized. Read more
Source§

fn by_ref(&self) -> &Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Allocator. Read more
Source§

impl<A, S> Allocator for Bump<A, S>

Available on crate feature allocator-api2-04 only.
Source§

fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, TargetAllocError>

Attempts to allocate a block of memory. Read more
Source§

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)

Deallocates the memory referenced by ptr. Read more
Source§

unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, TargetAllocError>

Attempts to extend the memory block. Read more
Source§

unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, TargetAllocError>

Behaves like grow, but also ensures that the new contents are set to zero before being returned. Read more
Source§

unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, TargetAllocError>

Attempts to shrink the memory block. Read more
Source§

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

Behaves like allocate, but also ensures that the returned memory is zero-initialized. Read more
Source§

fn by_ref(&self) -> &Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Allocator. Read more
Source§

impl<A, S> BumpAllocator for Bump<A, S>

Source§

type Allocator = A

The base allocator.
Source§

type Settings = S

The bump allocator settings.
Source§

fn as_scope(&self) -> &BumpScope<'_, Self::Allocator, Self::Settings>

Returns this bump allocator as a &BumpScope.
Source§

fn as_mut_scope( &mut self, ) -> &mut BumpScope<'_, Self::Allocator, Self::Settings>

Returns this bump allocator as a &mut BumpScope.
Source§

fn scope_guard(&mut self) -> BumpScopeGuard<'_, Self::Allocator, Self::Settings>

Creates a new BumpScopeGuard. Read more
Source§

fn scoped<R>( &mut self, f: impl FnOnce(&mut BumpScope<'_, Self::Allocator, Self::Settings>) -> R, ) -> R

Calls f with a new child scope. Read more
Source§

fn scoped_aligned<const NEW_MIN_ALIGN: usize, R>( &mut self, f: impl FnOnce(&mut BumpScope<'_, Self::Allocator, <Self::Settings as BumpAllocatorSettings>::WithMinimumAlignment<NEW_MIN_ALIGN>>) -> R, ) -> R

Calls f with a new child scope of a new minimum alignment. Read more
Source§

impl<A, S> BumpAllocatorCore for Bump<A, S>

Source§

fn any_stats(&self) -> AnyStats<'_>

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

fn checkpoint(&self) -> Checkpoint

Creates a checkpoint of the current bump position. Read more
Source§

unsafe fn reset_to(&self, checkpoint: Checkpoint)

Resets the bump position to a previously created checkpoint. The memory that has been allocated since then will be reused by future allocations. Read more
Source§

fn is_claimed(&self) -> bool

Returns true if the bump allocator is currently claimed.
Source§

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

Returns a pointer range of free space in the bump allocator with a size of at least layout.size(). Read more
Source§

unsafe fn allocate_prepared( &self, layout: Layout, range: Range<NonNull<u8>>, ) -> NonNull<u8>

Allocate part of the free space returned from a prepare_allocation call. Read more
Source§

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

Returns a pointer range of free space in the bump allocator with a size of at least layout.size(). Read more
Source§

unsafe fn allocate_prepared_rev( &self, layout: Layout, range: Range<NonNull<u8>>, ) -> NonNull<u8>

Allocate part of the free space returned from a prepare_allocation_rev call starting at the end. Read more
Source§

impl<A, S> BumpAllocatorTyped for Bump<A, S>

Source§

type TypedStats<'b> = Stats<'b, S> where Self: 'b

The type returned by the stats method.
Source§

fn typed_stats(&self) -> Self::TypedStats<'_>

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. Read more
Source§

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

A specialized version of allocate. Read more
Source§

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

A specialized version of allocate. Read more
Source§

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

A specialized version of allocate. Read more
Source§

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

A specialized version of allocate. Read more
Source§

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

A specialized version of allocate. Read more
Source§

fn allocate_slice_for<T>(&self, slice: &[T]) -> NonNull<T>

A specialized version of allocate. Read more
Source§

fn try_allocate_slice_for<T>( &self, slice: &[T], ) -> Result<NonNull<T>, AllocError>

A specialized version of allocate. Read more
Source§

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

A specialized version of shrink. Read more
Source§

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

A specialized version of prepare_allocation. Read more
Source§

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

A specialized version of prepare_allocation. Read more
Source§

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

A specialized version of allocate_prepared. Read more
Source§

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

A specialized version of prepare_allocation_rev. Read more
Source§

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

A specialized version of prepare_allocation_rev. Read more
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. Read more
Source§

fn reserve(&self, additional: usize)

Reserves capacity for at least additional more bytes to be bump allocated. The bump allocator may reserve more space to avoid frequent reallocations. After calling reserve, self.stats().remaining() will be greater than or equal to additional. Does nothing if the capacity is already sufficient. Read more
Source§

fn try_reserve(&self, additional: usize) -> Result<(), AllocError>

Reserves capacity for at least additional more bytes to be bump allocated. The bump allocator may reserve more space to avoid frequent reallocations. After calling reserve, self.stats().remaining() will be greater than or equal to additional. Does nothing if the capacity is already sufficient. Read more
Source§

fn dealloc<T: ?Sized>(&self, boxed: BumpBox<'_, T>)

Drops an allocated value and attempts to free its memory. Read more
Source§

impl<A, S> Debug for Bump<A, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A, S> Default for Bump<A, S>
where A: Allocator + Default, S: BumpAllocatorSettings<GuaranteedAllocated = False>,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<A, S> Drop for Bump<A, S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'b, A, S> From<&'b Bump<A, S>> for &'b BumpScope<'b, A, S>

Source§

fn from(value: &'b Bump<A, S>) -> Self

Converts to this type from the input type.
Source§

impl<'b, A, S> From<&'b mut Bump<A, S>> for &'b mut BumpScope<'b, A, S>

Source§

fn from(value: &'b mut Bump<A, S>) -> Self

Converts to this type from the input type.
Source§

impl<'a, A, S> BumpAllocatorCoreScope<'a> for &'a Bump<A, S>

Source§

impl<'a, A, S> BumpAllocatorCoreScope<'a> for &'a mut Bump<A, S>

Source§

impl<A, S> MutBumpAllocatorCore for Bump<A, S>

Source§

impl<A, S> RefUnwindSafe for Bump<A, S>

Source§

impl<A, S> Send for Bump<A, S>

Source§

impl<A, S> UnwindSafe for Bump<A, S>

Auto Trait Implementations§

§

impl<A = Global, S = BumpSettings> !Freeze for Bump<A, S>

§

impl<A = Global, S = BumpSettings> !Sync for Bump<A, S>

§

impl<A, S> Unpin for Bump<A, S>
where A: Unpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<A> MutBumpAllocatorTyped for A