Skip to main content

ObjectPool

Struct ObjectPool 

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

A thread-safe object pool.

Maintains a pool of reusable objects of type T. When an object is needed, it’s taken from the pool (or created if the pool is empty). When the object is dropped, it’s returned to the pool for reuse.

Implementations§

Source§

impl<T> ObjectPool<T>

Source

pub fn new<F>(factory: F) -> Self
where F: Fn() -> T + Send + Sync + 'static,

Creates a new object pool with the given factory function.

Source

pub fn with_reset<F, R>(factory: F, reset: R) -> Self
where F: Fn() -> T + Send + Sync + 'static, R: Fn(&mut T) + Send + Sync + 'static,

Creates a new object pool with a factory and reset function.

The reset function is called when an object is returned to the pool, allowing you to clear or reinitialize the object for reuse.

Source

pub fn with_max_size(self, max_size: usize) -> Self

Sets the maximum pool size.

Objects returned when the pool is at capacity will be dropped instead.

Source

pub fn prefill(&self, count: usize)

Pre-populates the pool with count objects.

Source

pub fn get(&self) -> Pooled<'_, T>

Takes an object from the pool, creating a new one if necessary.

Returns a Pooled wrapper that will return the object to the pool when dropped.

Source

pub fn take(&self) -> T

Takes an object from the pool without wrapping it.

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

Source

pub fn put(&self, value: T)

Returns an object to the pool.

If the pool is at capacity, the object is dropped instead.

Source

pub fn available(&self) -> usize

Returns the current number of objects in the pool.

Source

pub fn max_size(&self) -> usize

Returns the maximum pool size.

Source

pub fn clear(&self)

Clears all objects from the pool.

Source§

impl<T: 'static> ObjectPool<Vec<T>>

Source

pub fn new_vec_pool() -> Self

Creates a new vector pool.

Source

pub fn new_vec_pool_with_capacity(capacity: usize) -> Self

Creates a new vector pool with pre-allocated capacity.

Auto Trait Implementations§

§

impl<T> !Freeze for ObjectPool<T>

§

impl<T> !RefUnwindSafe for ObjectPool<T>

§

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

§

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

§

impl<T> Unpin for ObjectPool<T>
where T: Unpin,

§

impl<T> !UnwindSafe for ObjectPool<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.