pub struct Pool<T: Default + Reset> { /* private fields */ }
Expand description
A lock-free, thread-safe object pool.
Implementations§
Source§impl<T: Default + Reset> Pool<T>
impl<T: Default + Reset> Pool<T>
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create a new pool with the specified capacity.
Note: The capacity will be fully allocated.
§Panics
Panics if the capacity is 0
.
Sourcepub fn take(&self) -> Pooled<T>
pub fn take(&self) -> Pooled<T>
Take an object from the pool, creating a new one if none are available.
let pool: Pool<String> = Pool::new(69);
let mut string: Pooled<String> = pool.take();
// do something with it...
Sourcepub fn try_take(&self) -> Option<Pooled<T>>
pub fn try_take(&self) -> Option<Pooled<T>>
Take an object from the pool, returning None
if none are available.
This will never allocate.
let pool = Pool::new(69);
assert!(pool.try_take().is_none());
// add an object to the pool
let foo = pool.take();
drop(foo);
assert!(pool.try_take().is_some());
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
The number of available objects in the pool.
let pool = Pool::new(69);
// add 3 objects to the pool
let foo = pool.take();
let bar = pool.take();
let baz = pool.take();
drop((foo, bar, baz));
assert_eq!(pool.len(), 3);
Sourcepub fn in_use(&self) -> usize
pub fn in_use(&self) -> usize
The number of objects currently being used.
let pool = Pool::new(69);
// use 3 objects
let foo = pool.take();
let bar = pool.take();
let baz = pool.take();
assert_eq!(pool.in_use(), 3);
// return them
drop((foo, bar, baz));
assert_eq!(pool.in_use(), 0);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the pool is empty.
let pool = Pool::new(69);
assert!(pool.is_empty());
// add an object to the pool
let foo = pool.take();
drop(foo);
assert_eq!(pool.is_empty(), false);
// take it back out
let foo = pool.take();
assert!(pool.is_empty());
Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Whether the pool is full.
let pool = Pool::new(1);
assert_eq!(pool.is_full(), false);
// add an object to the pool
let foo = pool.take();
drop(foo);
assert!(pool.is_full());
// take it back out
let foo = pool.take();
assert_eq!(pool.is_full(), false);
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
The maximum capacity of the pool.
let pool = Pool::new(69);
assert_eq!(pool.capacity(), 69);
Sourcepub fn spare_capacity(&self) -> usize
pub fn spare_capacity(&self) -> usize
The spare capacity of the pool.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Pool<T>
impl<T> RefUnwindSafe for Pool<T>
impl<T> Send for Pool<T>where
T: Send,
impl<T> Sync for Pool<T>where
T: Send,
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