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>
impl<A, const MIN_ALIGN: usize, const UP: bool> BumpPool<A, MIN_ALIGN, UP>
sourcepub const fn new_in(allocator: A) -> Self
pub const fn new_in(allocator: A) -> Self
Constructs a new BumpPool
with the provided allocator.
sourcepub fn get(&self) -> BumpPoolGuard<'_, A, MIN_ALIGN, UP>
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.
sourcepub fn try_get(&self) -> Result<BumpPoolGuard<'_, A, MIN_ALIGN, UP>, AllocError>
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§
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>
impl<A, const MIN_ALIGN: usize, const UP: bool> Send for BumpPool<A, MIN_ALIGN, UP>
impl<A, const MIN_ALIGN: usize, const UP: bool> Sync for BumpPool<A, MIN_ALIGN, UP>
impl<A, const MIN_ALIGN: usize, const UP: bool> Unpin for BumpPool<A, MIN_ALIGN, UP>
impl<A, const MIN_ALIGN: usize, const UP: bool> UnwindSafe for BumpPool<A, MIN_ALIGN, UP>
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