Struct Pool

Source
pub struct Pool<T> { /* private fields */ }
Expand description

A shared resource pool

Values are acquired using Pool::acquire and returned when the Guard is dropped.

§Examples

use tub::Pool;
use std::net::UdpSocket;
#[tokio::main]
async fn main() {
 // Create a pool of UDP sockets
 let pool: Pool<UdpSocket> = (0..10)
      .map(|_| UdpSocket::bind("127.0.0.1:0").unwrap())
      .into();
  
  // Get a socket from the pool
  let mut socket = pool.acquire().await;
}

Implementations§

Source§

impl<T> Pool<T>

Source

pub async fn acquire(&self) -> Guard<T>

Acquire a value from the pool.

The value is protected by a Guard

§Examples
use tub::Pool;

#[tokio::main]
async fn main() {
   let pool: Pool<u32> = Pool::from_default(10);
   let mut box1 = pool.acquire().await;
   assert_eq!(pool.remaining_capacity(), 9);
   assert_eq!(*box1, u32::default());
}
Source

pub fn remaining_capacity(&self) -> usize

Get the number of available values in the pool

§Examples
use tub::Pool;
let pool = Pool::from_iter(0..10);
assert_eq!(pool.remaining_capacity(), 10);
Source

pub fn from_vec(vec: Vec<T>) -> Pool<T>

Create a new pool from a vector of values

§Examples
use tub::Pool;
let pool = Pool::from_vec(vec![1, 2, 3]);
Source

pub fn from_initializer<F>(capacity: usize, init: F) -> Pool<T>
where F: Fn() -> T,

Create a new pool from an initializer.

The initializer is called once for each value in the pool.

§Examples
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;
use tub::Pool;
let pool = Pool::from_initializer(10, || {
    static COUNTER: AtomicUsize = AtomicUsize::new(0);
    COUNTER.fetch_add(1, SeqCst);
});
Source

pub fn from_iter<I>(iterable: I) -> Pool<T>
where I: IntoIterator<Item = T>,

Create a new pool from an iterator

§Examples
use tub::Pool;
let pool = Pool::from_iter(0..10);
Source§

impl<T> Pool<T>
where T: Default,

Source

pub fn from_default(capacity: usize) -> Pool<T>
where T: Default,

Create a new pool with a default value

§Examples
use tub::Pool;
let pool: Pool<u32> = Pool::from_default(10);
Source§

impl<T> Pool<T>
where T: Copy,

Source

pub fn from_copy(capacity: usize, value: T) -> Pool<T>
where T: Copy,

Create a new pool with a copy of a value

§Examples
use tub::Pool;
let pool = Pool::from_copy(10, 123);
Source§

impl<T> Pool<T>
where T: Clone,

Source

pub fn from_clone(capacity: usize, value: &T) -> Pool<T>
where T: Clone,

Create a new pool with a clone of a value

§Examples
use tub::Pool;
let pool = Pool::from_clone(10, &123);

Trait Implementations§

Source§

impl<T> Clone for Pool<T>
where T: Clone,

Source§

fn clone(&self) -> Pool<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T, I> From<I> for Pool<T>
where T: Send + Sync + 'static, I: IntoIterator<Item = T>,

Source§

fn from(iter: I) -> Pool<T>

§Examples
use tub::Pool;

#[tokio::main]
async fn main() {
  let pool: Pool<u32> = (0..10).into();
}

Auto Trait Implementations§

§

impl<T> Freeze for Pool<T>

§

impl<T> RefUnwindSafe for Pool<T>

§

impl<T> Send for Pool<T>
where T: Send,

§

impl<T> Sync for Pool<T>
where T: Send,

§

impl<T> Unpin for Pool<T>

§

impl<T> UnwindSafe for Pool<T>

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V