[−][src]Crate poule
A store of pre-initialized values.
Values can be checked out when needed, operated on, and will automatically be returned to the pool when they go out of scope. It can be used when handling values that are expensive to create. Based on the object pool pattern.
Example:
use poule::{Pool, Dirty}; use std::thread; let mut pool = Pool::with_capacity(20); pool.grow_to(10); let mut vec = pool.checkout(|| Dirty(Vec::with_capacity(16_384))).unwrap(); // Do some work with the value, this can happen in another thread thread::spawn(move || { for i in 0..10_000 { vec.push(i); } assert_eq!(10_000, vec.len()); }).join(); // The vec will have been returned to the pool by now let vec = pool.checkout(|| Dirty(Vec::with_capacity(16_384))).unwrap(); // The pool operates LIFO, so this vec will be the same value that was used // in the thread above. The value will also be left as it was when it was // returned to the pool, this may or may not be desirable depending on the // use case. assert_eq!(10_000, vec.len());
Threading
Checking out values from the pool requires a mutable reference to the pool
so cannot happen concurrently across threads, but returning values to the
pool is thread safe and lock free, so if the value being pooled is Sync
then Checkout<T>
is Sync
as well.
The easiest way to have a single pool shared across many threads would be
to wrap Pool
in a mutex.
Structs
Checkout | A handle to a checked out value. When dropped out of scope, the value will be returned to the pool. |
Dirty | |
Pool | A pool of reusable values |
Traits
Reset | Resetting an object reverts that object back to a default state. |