Struct bump_scope::BumpPool

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

A pool of bump allocators.

This type allows you to do bump allocations from different threads that have their lifetime 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<const MIN_ALIGN: usize, const UP: bool, const INIT: bool> BumpPool<Global, MIN_ALIGN, UP, INIT>

source

pub fn new() -> Self

Available on crate feature alloc only.

Constructs a new BumpPool.

source§

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

source

pub 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, INIT>>

Returns the vector of Bumps.

source

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

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, INIT>, 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, const INIT: bool> Debug for BumpPool<A, MIN_ALIGN, UP, INIT>

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, const INIT: bool> Default for BumpPool<A, MIN_ALIGN, UP, INIT>

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, const INIT: bool = true> !Freeze for BumpPool<A, MIN_ALIGN, UP, INIT>

§

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

§

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

§

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

§

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

§

impl<A, const MIN_ALIGN: usize, const UP: bool, const INIT: bool> UnwindSafe for BumpPool<A, MIN_ALIGN, UP, INIT>
where 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>,

§

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

§

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.