Struct swimmer::Pool[][src]

pub struct Pool<T> where
    T: Recyclable
{ /* fields omitted */ }

A thread-safe object pool, used to reuse objects without reallocating.

See the crate-level documentation for more information.

Implementations

impl<T> Pool<T> where
    T: Recyclable
[src]

pub fn new() -> Pool<T>[src]

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...

pub fn with_size(size: usize) -> Pool<T>[src]

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);

pub fn get(&self) -> Recycled<'_, T>[src]

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, "");

pub fn size(&self) -> usize[src]

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);

pub fn attach(&self, value: T) -> Recycled<'_, T>[src]

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);

pub fn detached(&self) -> T[src]

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);

Trait Implementations

impl<T: Default> Default for Pool<T> where
    T: Recyclable
[src]

Auto Trait Implementations

impl<T> !RefUnwindSafe for Pool<T>[src]

impl<T> Send for Pool<T>[src]

impl<T> Sync for Pool<T>[src]

impl<T> Unpin for Pool<T> where
    T: Unpin
[src]

impl<T> !UnwindSafe for Pool<T>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.