Pool

Struct Pool 

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

A bounded pool where one thread acquires and any thread can return.

Only one Pool exists per pool. It cannot be cloned or shared across threads (it is Send but not Sync or Clone).

When the Pool is dropped, outstanding Pooled guards will drop their values directly instead of returning them to the pool.

§Example

use nexus_pool::sync::Pool;

let acquirer = Pool::new(
    100,
    || Vec::<u8>::with_capacity(1024),
    |v| v.clear(),
);

// Acquirer thread
let mut buf = acquirer.try_acquire().unwrap();
buf.extend_from_slice(b"hello");

// Can send buf to another thread
std::thread::spawn(move || {
    println!("{:?}", &*buf);
    // buf returns to pool on drop
}).join().unwrap();

Implementations§

Source§

impl<T> Pool<T>

Source

pub fn new<I, R>(capacity: usize, init: I, reset: R) -> Self
where I: FnMut() -> T, R: Fn(&mut T) + Send + Sync + 'static,

Creates a pool with capacity pre-initialized objects.

§Arguments
  • capacity - Number of objects to pre-allocate
  • init - Factory function to create each object
  • reset - Called when object returns to pool (e.g., Vec::clear)
§Panics

Panics if capacity is zero or exceeds u32::MAX - 1.

Source

pub fn try_acquire(&self) -> Option<Pooled<T>>

Attempts to acquire an object from the pool.

Returns None if all objects are currently in use.

Source

pub fn available(&self) -> usize

Returns the number of available objects.

Note: This is a snapshot and may be immediately outdated if other threads are returning objects concurrently.

Trait Implementations§

Source§

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

Auto Trait Implementations§

§

impl<T> Freeze for Pool<T>

§

impl<T> !RefUnwindSafe for Pool<T>

§

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

§

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