piscina 0.1.0

A simple generic pool that supports both sync and async
Documentation

piscina

A simple generic pool that supports both sync and async.

crate_version docs_version

This crate provides a simple generic pool that uses channels instead of locks and supports both sync and async without dependence on any runtime.

Features

default = []

The features below are related to logging errors, which should be unreachable.

  • log: Enables logging using the log crate.
  • tracing: Enables logging using the tracing crate.

Examples

Non-async 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);
let item3 = pool.try_get();
assert!(item3.is_some());

Async 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);
    let item3 = pool.get().await;
});

Safety

Instead of using an Option, this crate uses unsafe code ManuallyDrop in PooledItem. And the only use of unsafe code ManuallyDrop::take() occurs when PooledItem is dropped.

TODO:

  • Allow removal of items with pop() and try_pop() once an item becomes available

License: MIT/Apache-2.0