Struct piscina::pool::Pool

source ·
pub struct Pool<T> { /* private fields */ }
Expand description

A pool of generic items

Internally it maintains two lists: one of ready items and one of borrowed

Examples

Non-async Pool example:

use piscina::Pool;

let mut pool = Pool::new();
pool.put(1);
pool.put(2);

let item1 = pool.try_get();
assert!(item1.is_some());

let item2 = pool.blocking_get();

let none = pool.try_get();
assert!(none.is_none());

drop(item1); // Return the item to the pool by dropping it
let item3 = pool.try_get();
assert!(item3.is_some());

Async Pool example:

use piscina::Pool;

futures::executor::block_on(async {
    let mut pool = Pool::new();
    pool.put(1);
    pool.put(2);

    let item1 = pool.get().await;
    let item2 = pool.get().await;

    let none = pool.try_get();
    assert!(none.is_none());

    drop(item1); // Return the item to the pool by dropping it
    let item3 = pool.get().await;
});

Implementations§

source§

impl<T> Pool<T>

source

pub fn new() -> Self

Creates an empty pool

source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty pool with the specified capacity for both the ready and borrowed lists.

source

pub fn len(&self) -> usize

Returns the number of all items, whether ready or borrowed.

source

pub fn is_empty(&self) -> bool

Returns true if the pool contains no items.

source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of both the ready and borrowed lists as much as possible.

source

pub fn put(&mut self, item: T)

Puts an item into the pool

Example
use piscina::Pool;
 
let mut pool = Pool::new();
pool.put(1);
source

pub fn try_get(&mut self) -> Option<PooledItem<T>>

Tries to get an item from the pool. Returns None if there is no item immediately available.

Please note that if the sender is dropped before the item is returned to the pool, the item will be lost and this will be reflected on the pool’s length.

Example
use piscina::Pool;
 
let mut pool = Pool::new();
pool.put(1);
let item = pool.try_get();
assert!(item.is_some());
source

pub fn blocking_get(&mut self) -> PooledItem<T>

Gets an item from the pool. If there is no item immediately available, this will wait in a blocking manner until an item is available.

Example
use piscina::Pool;
 
let mut pool = Pool::new();
pool.put(1);
let item = pool.blocking_get();
Panic

This will panic if the sender is dropped before the item is returned to the pool, which should never happen.

source

pub fn get(&mut self) -> Get<'_, T>

Gets an item from the pool. If there is no item immediately available, the future will .await until one is available.

Example
use piscina::Pool;
 
let mut pool = Pool::new();
pool.put(1);
futures::executor::block_on(async {
    let item = pool.get().await;
});
Panic

This future will panic if any sender is dropped before the item is returned to the pool, which should never happen.

source

pub fn try_pop(&mut self) -> Option<T>

Tries to pop an item from the pool. Returns None if there is no item immediately available.

Please note that if the sender is dropped before the item is returned to the pool, the item will be lost and this will be reflected on the pool’s length.

Example
use piscina::Pool;
 
let mut pool = Pool::new();
pool.put(1);
assert_eq!(pool.len(), 1);
 
let item = pool.try_pop();
assert!(item.is_some());
assert_eq!(pool.len(), 0);
source

pub fn blocking_pop(&mut self) -> T

Pops an item from the pool. If there is no item immediately available, this will wait in a blocking manner until an item is available.

Example
use piscina::Pool;
 
let mut pool = Pool::new();
pool.put(1);
assert_eq!(pool.len(), 1);
let item = pool.blocking_pop();
assert_eq!(pool.len(), 0);
Panic

This will panic if the sender is dropped before the item is returned to the pool, which should never happen.

source

pub fn pop(&mut self) -> Pop<'_, T>

Pops an item from the pool. If there is no item immediately available, this will wait in a blocking manner until an item is available.

Example
use piscina::Pool;
 
let mut pool = Pool::new();
pool.put(1);
assert_eq!(pool.len(), 1);
futures::executor::block_on(async {
   let item = pool.pop().await;
   assert_eq!(pool.len(), 0);
});
Panic

This will panic if the sender is dropped before the item is returned to the pool, which should never happen.

Trait Implementations§

source§

impl<T: Debug> Debug for Pool<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Default> Default for Pool<T>

source§

fn default() -> Pool<T>

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

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Pool<T>

§

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

§

impl<T> Sync for Pool<T>where T: Send + Sync,

§

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

§

impl<T> !UnwindSafe for Pool<T>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.