Skip to main content

BumpPool

Struct BumpPool 

Source
pub struct BumpPool<A = Global, S = BumpSettings>{ /* 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, S> BumpPool<A, S>

Source

pub fn new() -> Self

Constructs a new BumpPool.

Source§

impl<A, S> BumpPool<A, S>

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

Returns the vector of Bumps.

Source§

impl<A, S> BumpPool<A, S>

Source

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

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().

Source

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

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

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, S>, 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, S> Debug for BumpPool<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 BumpPool<A, S>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

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

§

impl<A, S> RefUnwindSafe for BumpPool<A, S>
where A: RefUnwindSafe,

§

impl<A, S> Send for BumpPool<A, S>
where A: Send,

§

impl<A, S> Sync for BumpPool<A, S>
where A: Sync,

§

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

§

impl<A, S> UnwindSafe for BumpPool<A, S>
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>,

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.