pub struct Pool<T>where
T: Recyclable,{ /* private fields */ }
Expand description
A thread-safe object pool, used to reuse objects without reallocating.
See the crate-level documentation for more information.
Implementations§
Source§impl<T> Pool<T>where
T: Recyclable,
impl<T> Pool<T>where
T: Recyclable,
Sourcepub fn new() -> Pool<T>
pub fn new() -> Pool<T>
Creates a new pool with default settings.
This is equivalent to swimmer::builder().build()
.
§Examples
use swimmer::Pool;
let pool: Pool<String> = Pool::new();
// Use the pool...
Sourcepub fn with_size(size: usize) -> Pool<T>
pub fn with_size(size: usize) -> Pool<T>
Creates a new pool with the specified
starting size. The pool will allocate
size
initial values and insert them into
the pool.
This is equivalent to swimmer::builder().with_size(size).build()
.
§Examples
use swimmer::Pool;
let pool: Pool<Vec<String>> = Pool::with_size(16);
assert_eq!(pool.size(), 16);
Sourcepub fn get(&self) -> Recycled<'_, T>
pub fn get(&self) -> Recycled<'_, T>
Retrieves a value from the pool.
The value
is returned using a Recycled
smart pointer
which returns the object to the pool when dropped.
§Examples
use swimmer::Pool;
let pool: Pool<String> = Pool::new();
let string = pool.get();
assert_eq!(*string, "");
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the current size of the pool.
When an object is removed from the pool, the size is decremented; when it is returned, the size is incremented.
§Examples
use swimmer::Pool;
let pool: Pool<String> = Pool::with_size(16);
assert_eq!(pool.size(), 16);
let _string = pool.get();
assert_eq!(pool.size(), 15);
drop(_string);
assert_eq!(pool.size(), 16);
Sourcepub fn attach(&self, value: T) -> Recycled<'_, T>
pub fn attach(&self, value: T) -> Recycled<'_, T>
Attaches value
to this pool, wrapping
it in a smart pointer which will return the
object into the pool when dropped.
§Examples
use swimmer::Pool;
let pool: Pool<u64> = Pool::with_size(0);
assert_eq!(pool.size(), 0);
let ten = pool.attach(10);
// `ten` is still borrowed from the pool,
// so the size hasn't changed
assert_eq!(pool.size(), 0);
// When dropped, `ten` will be returned
// back to the pool
drop(ten);
assert_eq!(pool.size(), 1);
Sourcepub fn detached(&self) -> T
pub fn detached(&self) -> T
Detatches a value from this pool.
This is equivalent to get
, except
for that the object will not be returned
to the pool when dropped—it will simply be dropped.
§Examples
use swimmer::Pool;
let pool: Pool<String> = Pool::with_size(10);
let detached_string = pool.detached();
assert_eq!(pool.size(), 9);
// When dropped, the string won't
// be returned to the pool
drop(detached_string);
assert_eq!(pool.size(), 9);