pub struct ObjectPool<T> { /* private fields */ }Expand description
A thread-safe object pool.
Maintains a pool of reusable objects of type T. When an object is needed, it’s taken from the pool (or created if the pool is empty). When the object is dropped, it’s returned to the pool for reuse.
Implementations§
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.