Struct bump_scope::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 ints: Vec<&mut usize> = (0..1000)
    .into_par_iter()
    .map_init(|| pool.get(), |bump, i| {
        // do some expensive work
        bump.alloc(i).into_mut()
    })
    .collect();

dbg!(&ints);

pool.reset();

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

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

Returns the vector of Bumps.

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.

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

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