pub struct BumpPool<A = Global, const MIN_ALIGN: usize = 1, const UP: bool = true>{ /* private fields */ }
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, 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 fn get(&self) -> BumpPoolGuard<'_, A, MIN_ALIGN, UP>
pub fn get(&self) -> BumpPoolGuard<'_, A, MIN_ALIGN, UP>
Sourcepub fn try_get(&self) -> Result<BumpPoolGuard<'_, A, MIN_ALIGN, UP>, AllocError>
pub fn try_get(&self) -> Result<BumpPoolGuard<'_, A, MIN_ALIGN, UP>, AllocError>
Sourcepub fn get_with_size(&self, size: usize) -> BumpPoolGuard<'_, A, MIN_ALIGN, UP>
pub fn get_with_size(&self, size: usize) -> BumpPoolGuard<'_, A, MIN_ALIGN, UP>
Sourcepub fn try_get_with_size(
&self,
size: usize,
) -> Result<BumpPoolGuard<'_, A, MIN_ALIGN, UP>, AllocError>
pub fn try_get_with_size( &self, size: usize, ) -> 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.
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.
Sourcepub fn get_with_capacity(
&self,
layout: Layout,
) -> BumpPoolGuard<'_, A, MIN_ALIGN, UP>
pub fn get_with_capacity( &self, layout: Layout, ) -> 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.
If this needs to create a new Bump
, it will be constructed by calling Bump::with_capacity(layout)
.
§Panics
Panics if the allocation fails.
Sourcepub fn try_get_with_capacity(
&self,
layout: Layout,
) -> Result<BumpPoolGuard<'_, A, MIN_ALIGN, UP>, AllocError>
pub fn try_get_with_capacity( &self, layout: Layout, ) -> 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.
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.