pub struct Pooled<M: Manager> { /* private fields */ }Available on crate feature
std only.Expand description
A borrowed resource that returns itself to the pool when dropped.
Obtained from Pool::get and
Pool::get_timeout. A Pooled<M> deref-coerces
to the underlying resource, so it can be used anywhere a &M::Resource or
&mut M::Resource is expected. When the guard goes out of scope the resource
is recycled (via Manager::recycle) and returned
to the idle set — there is no release call to remember, and no way to leak a
resource by forgetting one.
If recycling fails, or the pool has been closed, the resource is dropped instead of pooled and its slot is freed for a replacement.
The guard is Send whenever the resource is, so it may be held across
.await points in async code.
§Examples
use pool_mod::{Manager, Pool};
use std::convert::Infallible;
struct Counters;
impl Manager for Counters {
type Resource = u64;
type Error = Infallible;
fn create(&self) -> Result<u64, Infallible> { Ok(0) }
fn recycle(&self, _r: &mut u64) -> Result<(), Infallible> { Ok(()) }
}
let pool = Pool::builder(Counters).max_size(2).build()
.expect("configuration is valid");
{
let mut n = pool.get().expect("a resource is available");
*n += 1; // DerefMut to the pooled u64
assert_eq!(*n, 1);
} // `n` is recycled and returned to the pool here
assert_eq!(pool.status().idle, 1);Trait Implementations§
Auto Trait Implementations§
impl<M> Freeze for Pooled<M>
impl<M> RefUnwindSafe for Pooled<M>
impl<M> Send for Pooled<M>
impl<M> Sync for Pooled<M>
impl<M> Unpin for Pooled<M>
impl<M> UnsafeUnpin for Pooled<M>
impl<M> UnwindSafe for Pooled<M>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more