pub struct Pool<T> { /* private fields */ }Expand description
A bounded pool where one thread acquires and any thread can return.
Only one Pool exists per pool. It cannot be cloned or shared
across threads (it is Send but not Sync or Clone).
When the Pool is dropped while Pooled guards are still alive,
the guards keep Inner alive via a strong Arc. Each guard, on
drop, returns its value to the (now-orphaned) free list; the last
guard to drop releases Inner, which drops every in-pool slot.
See docs/caveats.md §2.
§Example
use nexus_pool::sync::Pool;
let acquirer = Pool::new(
100,
|| Vec::<u8>::with_capacity(1024),
|v| v.clear(),
);
// Acquirer thread
let mut buf = acquirer.try_acquire().unwrap();
buf.extend_from_slice(b"hello");
// Can send buf to another thread
std::thread::spawn(move || {
println!("{:?}", &*buf);
// buf returns to pool on drop
}).join().unwrap();Implementations§
Source§impl<T> Pool<T>
impl<T> Pool<T>
Sourcepub fn new<I, R>(capacity: usize, init: I, reset: R) -> Self
pub fn new<I, R>(capacity: usize, init: I, reset: R) -> Self
Creates a pool with capacity pre-initialized objects.
§Arguments
capacity- Number of objects to pre-allocateinit- Factory function to create each objectreset- Called when object returns to pool (e.g.,Vec::clear)
§Panics
Panics if capacity is zero or exceeds usize::MAX - 1.
The reset closure must not panic. If it does, the value is leaked
and the pool slot is not returned. Use simple operations like
Vec::clear() or field resets.
Sourcepub fn try_acquire(&self) -> Option<Pooled<T>>
pub fn try_acquire(&self) -> Option<Pooled<T>>
Attempts to acquire an object from the pool.
Returns None if all objects are currently in use.