piscina
A simple generic pool that supports both sync and async.
Two types of pools are provided:
- [
Pool]: A simple pool that supports both sync and async. - [
PriorityPool]: A pool of items with priorities and supports both sync and async.
This crate internally utilizes futures_channel::oneshot and is thus lock-free.
Features
= []
The features below are related to logging errors, which should be unreachable.
log: Enables logging using thelogcrate.tracing: Enables logging using thetracingcrate.
Examples
Non-async [Pool] example:
use Pool;
let mut pool = new;
pool.put;
pool.put;
let item1 = pool.try_get;
assert!;
let item2 = pool.blocking_get;
let none = pool.try_get;
assert!;
drop; // Return the item to the pool by dropping it
let item3 = pool.try_get;
assert!;
Async [Pool] example:
use Pool;
block_on;
Non-async [PriorityPool] example:
use PriorityPool;
let mut pool = new;
pool.put;
pool.put; // Larger value means higher priority.
// Get the highest priority item.
let item = pool.try_get;
assert!;
let item = item.unwrap;
assert_eq!;
let another_item = pool.try_get;
assert!;
let another_item = another_item.unwrap;
assert_eq!;
// The pool is now empty.
let empty_item = pool.try_get;
assert!;
drop; // Return the item back to the pool by dropping it.
let hello = pool.try_get;
assert!;
assert_eq!;
Async [PriorityPool] example:
use PriorityPool;
let mut pool = new;
pool.put;
pool.put; // Larger value means higher priority.
block_on;
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.
License: MIT/Apache-2.0