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>where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
impl<const MIN_ALIGN: usize, const UP: bool, const INIT: bool> BumpPool<Global, MIN_ALIGN, UP, INIT>where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
source§impl<A, const MIN_ALIGN: usize, const UP: bool, const INIT: bool> BumpPool<A, MIN_ALIGN, UP, INIT>
impl<A, const MIN_ALIGN: usize, const UP: bool, const INIT: bool> BumpPool<A, MIN_ALIGN, UP, INIT>
sourcepub fn get(&self) -> BumpPoolGuard<'_, A, MIN_ALIGN, UP, INIT>
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.
sourcepub fn try_get(
&self
) -> Result<BumpPoolGuard<'_, A, MIN_ALIGN, UP, INIT>, AllocError>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more