Skip to main content

Pooled

Struct Pooled 

Source
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§

Source§

impl<M: Manager> Deref for Pooled<M>

Source§

type Target = <M as Manager>::Resource

The resulting type after dereferencing.
Source§

fn deref(&self) -> &M::Resource

Dereferences the value.
Source§

impl<M: Manager> DerefMut for Pooled<M>

Source§

fn deref_mut(&mut self) -> &mut M::Resource

Mutably dereferences the value.
Source§

impl<M: Manager> Drop for Pooled<M>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<M> Freeze for Pooled<M>
where <M as Manager>::Resource: Freeze,

§

impl<M> RefUnwindSafe for Pooled<M>

§

impl<M> Send for Pooled<M>

§

impl<M> Sync for Pooled<M>
where <M as Manager>::Resource: Sync,

§

impl<M> Unpin for Pooled<M>
where <M as Manager>::Resource: Unpin,

§

impl<M> UnsafeUnpin for Pooled<M>
where <M as Manager>::Resource: UnsafeUnpin,

§

impl<M> UnwindSafe for Pooled<M>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.