BumpPool

Struct BumpPool 

Source
pub struct BumpPool<A = Global, const MIN_ALIGN: usize = 1, const UP: bool = true>{ /* private fields */ }
Available on crate feature std only.
Expand description

A pool of bump allocators.

This type allows bump allocations in parallel, with the allocations’ lifetimes tied to the pool.

§Examples

Using BumpPool with parallel iterators from rayon:

let mut pool: BumpPool = BumpPool::new();

let strings: Vec<&str> = (0..1000)
    .into_par_iter()
    .map_init(|| pool.get(), |bump, i| {
        // do some expensive work
        bump.alloc_fmt(format_args!("{i}")).into_ref()
    })
    .collect();

dbg!(&strings);

pool.reset();

// memory of the strings is freed, trying to access `strings` will result in a lifetime error
// dbg!(&strings);

Using BumpPool with std::thread::scope:

let pool: BumpPool = BumpPool::new();
let (sender, receiver) = std::sync::mpsc::sync_channel(10);

std::thread::scope(|s| {
    s.spawn(|| {
        let bump = pool.get();
        let string = bump.alloc_str("Hello");
        sender.send(string).unwrap();
        drop(sender);
    });

    s.spawn(|| {
        for string in receiver {
            assert_eq!(string, "Hello");
        }
    });
});

Implementations§

Source§

impl<A, const MIN_ALIGN: usize, const UP: bool> BumpPool<A, MIN_ALIGN, UP>

Source

pub fn new() -> Self

Constructs a new BumpPool.

Source§

impl<A, const MIN_ALIGN: usize, const UP: bool> BumpPool<A, MIN_ALIGN, UP>

Source

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

Constructs a new BumpPool with the provided allocator.

Source

pub fn reset(&mut self)

Resets all Bumps in this pool.

Source

pub fn bumps(&mut self) -> &mut Vec<Bump<A, MIN_ALIGN, UP, true, true>>

Returns the vector of Bumps.

Source§

impl<A, const MIN_ALIGN: usize, const UP: bool> BumpPool<A, MIN_ALIGN, UP>

Source

pub fn get(&self) -> BumpPoolGuard<'_, A, MIN_ALIGN, UP>

Borrows a bump allocator from the pool. With this BumpPoolGuard you can make allocations that live for as long as the pool lives.

If this needs to create a new Bump, it will be constructed by calling Bump::new().

§Panics

Panics if the allocation fails.

Source

pub fn try_get(&self) -> Result<BumpPoolGuard<'_, A, MIN_ALIGN, UP>, AllocError>

Borrows a bump allocator from the pool. With this BumpPoolGuard you can make allocations that live for as long as the pool lives.

If this needs to create a new Bump, it will be constructed by calling Bump::try_new().

§Errors

Errors if the allocation fails.

Source

pub fn get_with_size(&self, size: usize) -> BumpPoolGuard<'_, A, MIN_ALIGN, UP>

Borrows a bump allocator from the pool. With this BumpPoolGuard you can make allocations that live for as long as the pool lives.

If this needs to create a new Bump, it will be constructed by calling Bump::with_size(size).

§Panics

Panics if the allocation fails.

Source

pub fn try_get_with_size( &self, size: usize, ) -> Result<BumpPoolGuard<'_, A, MIN_ALIGN, UP>, AllocError>

Borrows a bump allocator from the pool. With this BumpPoolGuard you can make allocations that live for as long as the pool lives.

If this needs to create a new Bump, it will be constructed by calling Bump::try_with_size(size).

§Errors

Errors if the allocation fails.

Source

pub fn get_with_capacity( &self, layout: Layout, ) -> BumpPoolGuard<'_, A, MIN_ALIGN, UP>

Borrows a bump allocator from the pool. With this BumpPoolGuard you can make allocations that live for as long as the pool lives.

If this needs to create a new Bump, it will be constructed by calling Bump::with_capacity(layout).

§Panics

Panics if the allocation fails.

Source

pub fn try_get_with_capacity( &self, layout: Layout, ) -> Result<BumpPoolGuard<'_, A, MIN_ALIGN, UP>, AllocError>

Borrows a bump allocator from the pool. With this BumpPoolGuard you can make allocations that live for as long as the pool lives.

If this needs to create a new Bump, it will be constructed by calling Bump::try_with_capacity(layout).

§Errors

Errors if the allocation fails.

Trait Implementations§

Source§

impl<A, const MIN_ALIGN: usize, const UP: bool> Debug for BumpPool<A, MIN_ALIGN, UP>

Source§

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

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

impl<A, const MIN_ALIGN: usize, const UP: bool> Default for BumpPool<A, MIN_ALIGN, UP>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<A = Global, const MIN_ALIGN: usize = 1, const UP: bool = true> !Freeze for BumpPool<A, MIN_ALIGN, UP>

§

impl<A, const MIN_ALIGN: usize, const UP: bool> RefUnwindSafe for BumpPool<A, MIN_ALIGN, UP>
where MinimumAlignment<MIN_ALIGN>: Sized, A: RefUnwindSafe,

§

impl<A, const MIN_ALIGN: usize, const UP: bool> Send for BumpPool<A, MIN_ALIGN, UP>
where MinimumAlignment<MIN_ALIGN>: Sized, A: Send,

§

impl<A, const MIN_ALIGN: usize, const UP: bool> Sync for BumpPool<A, MIN_ALIGN, UP>
where MinimumAlignment<MIN_ALIGN>: Sized, A: Sync,

§

impl<A, const MIN_ALIGN: usize, const UP: bool> Unpin for BumpPool<A, MIN_ALIGN, UP>
where MinimumAlignment<MIN_ALIGN>: Sized, A: Unpin,

§

impl<A, const MIN_ALIGN: usize, const UP: bool> UnwindSafe for BumpPool<A, MIN_ALIGN, UP>
where MinimumAlignment<MIN_ALIGN>: Sized, A: UnwindSafe,

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.