Struct Pool

Source
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,

Source

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

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

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

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

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

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

Trait Implementations§

Source§

impl<T> Default for Pool<T>
where T: Recyclable + Default,

Source§

fn default() -> Pool<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Pool<T>

§

impl<T> !RefUnwindSafe for Pool<T>

§

impl<T> Send for Pool<T>

§

impl<T> Sync for Pool<T>

§

impl<T> Unpin for Pool<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Pool<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.