pub struct ObjectPool<T> { /* private fields */ }Expand description
A thread-safe object pool for reusing allocations.
Use get() to grab an object (created fresh if the pool is
empty). When you drop the returned Pooled wrapper, the object goes
back to the pool for reuse.
§Examples
use grafeo_common::memory::ObjectPool;
// Pool of vectors that get cleared on return
let pool = ObjectPool::with_reset(Vec::<u8>::new, |v| v.clear());
let mut buf = pool.get();
buf.extend_from_slice(&[1, 2, 3]);
// buf is returned to pool when dropped, and clearedImplementations§
Source§impl<T> ObjectPool<T>
impl<T> ObjectPool<T>
Sourcepub fn with_reset<F, R>(factory: F, reset: R) -> Self
pub fn with_reset<F, R>(factory: F, reset: R) -> Self
Creates a new object pool with a factory and reset function.
The reset function is called when an object is returned to the pool, allowing you to clear or reinitialize the object for reuse.
Sourcepub fn with_max_size(self, max_size: usize) -> Self
pub fn with_max_size(self, max_size: usize) -> Self
Sets the maximum pool size.
Objects returned when the pool is at capacity will be dropped instead.
Sourcepub fn get(&self) -> Pooled<'_, T>
pub fn get(&self) -> Pooled<'_, T>
Takes an object from the pool, creating a new one if necessary.
Returns a Pooled wrapper that will return the object to the pool
when dropped.
Sourcepub fn take(&self) -> T
pub fn take(&self) -> T
Takes an object from the pool without wrapping it.
The caller is responsible for returning the object via put() if desired.
Source§impl<T: 'static> ObjectPool<Vec<T>>
impl<T: 'static> ObjectPool<Vec<T>>
Sourcepub fn new_vec_pool() -> Self
pub fn new_vec_pool() -> Self
Creates a new vector pool.
Sourcepub fn new_vec_pool_with_capacity(capacity: usize) -> Self
pub fn new_vec_pool_with_capacity(capacity: usize) -> Self
Creates a new vector pool with pre-allocated capacity.