Struct lease::init::InitPool

source ·
pub struct InitPool<T: Send + Sync + 'static, I: Init> { /* private fields */ }
Expand description

A pool of objects of type T that can be leased out. Just like a Pool, but it also carries an initializer that ensures each item is consistently initialized

Implementations§

source§

impl<T: Send + Sync + 'static, I: Init> InitPool<T, I>

source

pub fn try_into_locked_pool( self ) -> Result<LockedPool<T>, (Pool<T>, PoolConversionError)>

Turns this Pool into a LockedPool

§Errors

Will return an error if any of the variants in PoolConversionError apply

source

pub fn new(init: I) -> Self

Creates a new empty Pool

source

pub fn new_from<O: Into<I>>(init: O) -> Self

Creates a new empty Pool that will be initialized using init

source

pub fn init(&self) -> I::Output

Returns the output of the initializer so that consumers can get an initialized value that isn’t tied to the pool

source

pub fn try_get(&self) -> Option<Lease<T>>

Tries to get a Lease if one is available. This function does not block.

For an asynchronous version that returns when one is available use get()

source

pub async fn get(&self) -> Lease<T>

Returns a future that resolves to a Lease when one is available

Reqires the async feature to be enabled because it requires extra memory

source

pub fn stream(&self) -> impl Stream<Item = Lease<T>>

Returns a Stream of Leasees

source

pub fn try_get_or_new(&self) -> Lease<T>
where I::Output: Into<T>,

For the asynchronous version of this function see get_or_new()

Tries to get an existing Lease if available and if not returns a new one that has been added to the pool.

Calling this method repeatedly can cause the pool size to increase without bound.

source

pub async fn get_or_new(&self) -> Lease<T>
where I::Output: Future<Output = T>,

Asynchronous version of try_get_or_new()

Tries to get an existing Lease if available and if not returns a new one that has been added to the pool.

Calling this method repeatedly can cause the pool size to increase without bound.

source

pub fn try_get_or_try_new<E>(&self) -> Result<Lease<T>, E>
where I::Output: Into<Result<T, E>>,

For the asynchronous version of this function see get_or_try_new()

Tries to get an existing Lease if available and if not tries to create a new one that has been added to the pool.

Calling this method repeatedly can cause the pool size to increase without bound.

§Errors

Returns an error if the stored initializer errors

source

pub async fn get_or_try_new<E>(&self) -> Result<Lease<T>, E>
where I::Output: Future<Output = Result<T, E>>,

Asynchronous version of try_get_or_try_new()

Tries to get an existing Lease if available and if not tries to create a new one that has been added to the pool.

Calling this method repeatedly can cause the pool size to increase without bound.

§Errors

Returns an error if the stored initializer errors

source

pub fn try_get_or_new_with_cap(&self, cap: usize) -> Option<Lease<T>>
where I::Output: Into<T>,

For the asynchronous version of this function see get_or_new_with_cap()

Just like get_or_new() but caps the size of the pool. Once len() == cap then None is returned.

source

pub async fn get_or_new_with_cap(&self, cap: usize) -> Lease<T>
where I::Output: Future<Output = T>,

Asynchronous version of get_or_new_with_cap()

Just like get_or_new() but caps the size of the pool. Once len() == cap then it waits for an open lease.

source

pub fn try_get_or_try_new_with_cap<E>( &self, cap: usize ) -> Result<Option<Lease<T>>, E>
where I::Output: Into<Result<T, E>>,

For the asynchronous version of this function see get_or_try_new_with_cap()

Just like get_or_try_new() but caps the size of the pool. Once len() == cap then None is returned.

§Errors

Returns an error if the stored initializer errors

source

pub async fn get_or_try_new_with_cap<E>( &self, cap: usize ) -> Result<Lease<T>, E>
where I::Output: Future<Output = Result<T, E>>,

Asynchronous version of get_or_try_new_with_cap()

Just like get_or_try_new() but caps the size of the pool. Once len() == cap then it waits for an open lease.

§Errors

Returns an error if the stored initializer errors

source

pub fn len(&self) -> usize

Returns the size of the pool

source

pub fn clear(&self)

Sets the size of the pool to zero

This will disassociate all current Leasees and when they go out of scope the objects they’re holding will be dropped

source

pub fn available(&self) -> usize

Returns the number of currently available Leasees. Even if the return is non-zero, calling get() immediately afterward can still fail if multiple threads have access to this pool.

source

pub fn is_empty(&self) -> bool

Returns true if there are no items being stored.

source

pub fn disassociate(&self, lease: &Lease<T>)

Disassociates the Lease from this InitPool

Trait Implementations§

source§

impl<T: Send + Sync + 'static, I: Init> Clone for InitPool<T, I>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T, I> !RefUnwindSafe for InitPool<T, I>

§

impl<T, I> Send for InitPool<T, I>
where I: Sync + Send,

§

impl<T, I> Sync for InitPool<T, I>
where I: Sync + Send,

§

impl<T, I> Unpin for InitPool<T, I>

§

impl<T, I> !UnwindSafe for InitPool<T, I>

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<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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.