Struct tokio_utils::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>
impl<T> Pool<T>
sourcepub fn remaining_capacity(&self) -> usize
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);sourcepub fn from_vec(vec: Vec<T, Global>) -> Pool<T>
pub fn from_vec(vec: Vec<T, Global>) -> Pool<T>
Create a new pool from a vector of values
Examples
use tub::Pool;
let pool = Pool::from_vec(vec![1, 2, 3]);sourcepub fn from_initializer<F>(capacity: usize, init: F) -> Pool<T>where
F: Fn() -> T,
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);
});