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>) -> Pool<T>
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]);
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);
});
Sourcepub fn from_iter<I>(iterable: I) -> Pool<T>where
I: IntoIterator<Item = T>,
pub fn from_iter<I>(iterable: I) -> Pool<T>where
I: IntoIterator<Item = T>,
Trait Implementations§
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> 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