Skip to main content

Pool

Struct Pool 

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

Growable object pool with LIFO reuse.

Objects are created on demand via the factory function when the pool is empty. Use try_acquire() for the fast path that only returns pooled objects, or acquire() which may create new objects.

§Example

use nexus_pool::local::Pool;

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

let mut buf = pool.acquire(); // Creates new object
buf.extend_from_slice(b"hello");
drop(buf); // Returns to pool, clear() is called

let buf2 = pool.acquire(); // Reuses existing (now empty) object

Implementations§

Source§

impl<T> Pool<T>

Source

pub fn new<F, R>(factory: F, reset: R) -> Self
where F: FnMut() -> T + 'static, R: FnMut(&mut T) + 'static,

Creates an empty pool with the given factory and reset functions.

§Arguments
  • factory - Creates new objects when pool is empty
  • reset - Called when object returns to pool (e.g., Vec::clear)
Source

pub fn with_capacity<F, R>(capacity: usize, factory: F, reset: R) -> Self
where F: FnMut() -> T + 'static, R: FnMut(&mut T) + 'static,

Creates a pool pre-populated with capacity objects.

Source

pub fn acquire(&self) -> Pooled<T>

Acquires an object from the pool, creating one if necessary.

This always succeeds but may allocate if the pool is empty.

Source

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

Attempts to acquire an object from the pool without creating.

Returns None if the pool is empty. This is the fast path.

Source

pub fn take(&self) -> T

Takes an object from the pool without an RAII guard, creating one via the factory if the pool is empty.

The caller is responsible for returning the object via put().

§Example
use nexus_pool::local::Pool;

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

let mut buf = pool.take();
buf.extend_from_slice(b"hello");
pool.put(buf); // manual return, reset is called
Source

pub fn try_take(&self) -> Option<T>

Takes an object from the pool if one is available, without creating.

Returns None if the pool is empty. The caller is responsible for returning the object via put().

Source

pub fn put(&self, value: T)

Returns an object to the pool.

Calls the reset function, then pushes the value back onto the available stack for reuse.

Source

pub fn available(&self) -> usize

Returns the number of available objects.

Trait Implementations§

Source§

impl<T> Drop for Pool<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Pool<T>

§

impl<T> !RefUnwindSafe for Pool<T>

§

impl<T> !Send for Pool<T>

§

impl<T> !Sync for Pool<T>

§

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.