pub struct Pool<T> { /* private fields */ }
Expand description
Lock-free object pool.
Implementations§
Source§impl<T> Pool<T>
impl<T> Pool<T>
Sourcepub fn bounded(
capacity: usize,
new: impl Fn() -> T + Send + Sync + 'static,
reset: impl Fn(&mut T) + Send + Sync + 'static,
) -> Self
pub fn bounded( capacity: usize, new: impl Fn() -> T + Send + Sync + 'static, reset: impl Fn(&mut T) + Send + Sync + 'static, ) -> Self
Create a new pool with the given capacity.
§Example
use objectpool::Pool;
let pool = Pool::<u32>::bounded(10, Default::default, |_v| {});
Sourcepub fn unbounded(
new: impl Fn() -> T + Send + Sync + 'static,
reset: impl Fn(&mut T) + Send + Sync + 'static,
) -> Self
pub fn unbounded( new: impl Fn() -> T + Send + Sync + 'static, reset: impl Fn(&mut T) + Send + Sync + 'static, ) -> Self
Create a new pool with the unbounded capacity.
§Example
use objectpool::Pool;
let pool = Pool::<u32>::unbounded(Default::default, |_v| {});
Sourcepub fn get(&self) -> ReusableObjectRef<'_, T>
pub fn get(&self) -> ReusableObjectRef<'_, T>
Get an object from the pool.
§Example
use objectpool::Pool;
let pool = Pool::<u32>::bounded(10, Default::default, |_v| {});
let mut obj = pool.get();
assert_eq!(*obj, 0);
*obj = 42;
drop(obj);
Sourcepub fn get_owned(&self) -> ReusableObject<T>
pub fn get_owned(&self) -> ReusableObject<T>
Get an object from the pool.
§Example
use objectpool::Pool;
let pool = Pool::<u32>::bounded(10, Default::default, |_v| {});
let mut obj = pool.get_owned();
assert_eq!(*obj, 0);
*obj = 42;
drop(obj);
Sourcepub fn get_or_else(&self, fallback: impl Fn() -> T) -> ReusableObjectRef<'_, T>
pub fn get_or_else(&self, fallback: impl Fn() -> T) -> ReusableObjectRef<'_, T>
Get an object from the pool with a fallback.
§Example
use objectpool::Pool;
let pool = Pool::<u32>::bounded(10, Default::default, |_| {});
let mut obj = pool.get_or_else(|| 42);
assert_eq!(*obj, 42);
Sourcepub fn get_owned_or_else(&self, fallback: impl Fn() -> T) -> ReusableObject<T>
pub fn get_owned_or_else(&self, fallback: impl Fn() -> T) -> ReusableObject<T>
Get an object from the pool with a fallback.
§Example
use objectpool::Pool;
let pool = Pool::<u32>::bounded(10, Default::default, |_| {});
let mut obj = pool.get_owned_or_else(|| 42);
assert_eq!(*obj, 42);
Trait Implementations§
impl<T: Send> Send for Pool<T>
impl<T: Sync> Sync for Pool<T>
Auto Trait Implementations§
impl<T> Freeze for Pool<T>
impl<T> !RefUnwindSafe for Pool<T>
impl<T> Unpin for Pool<T>
impl<T> !UnwindSafe for Pool<T>
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