Struct Pool

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

Source

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.

Source

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

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

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

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

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

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

pub fn capacity(&self) -> usize

The maximum capacity of the pool.

let pool = Pool::new(69);
assert_eq!(pool.capacity(), 69);
Source

pub fn spare_capacity(&self) -> usize

The spare capacity of the pool.

Source

pub fn attach(&self, object: T) -> Pooled<T>

Attach an object to the pool.

Trait Implementations§

Source§

impl<T: Default + Reset> Clone for Pool<T>

This returns a reference to the same Pool.

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.