Skip to main content

ObjectPool

Struct ObjectPool 

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

Provides a thread-safe object pool with size limits and object validation.

Implementations§

Source§

impl<T> ObjectPool<T>

Source

pub const DEFAULT_MAX_SIZE: i32 = 32

The default of the maximum number of objects to keep in the pool.

Source

pub fn new( factory: impl Fn() -> T + Send + Sync + 'static, validator: Option<Box<dyn Fn(&mut T) -> bool + Send + Sync>>, max_size: i32, ) -> Self

Initializes a new instance of the pool.

factory creates new instances. The optional validator cleans and validates an object before it returns to the pool; it returns false to discard the object. max_size is the maximum number of objects to keep in the pool.

Panics when max_size is less than 1.

Source

pub fn rent(&self) -> T

Rents an object from the pool or creates a new one if the pool is empty.

Source

pub fn return_item(&self, item: T)

Returns an object to the pool if it passes validation and the pool is not full.

Callers must not return the same item more than once without an intervening rent. The pool does not detect duplicate returns. The pool also does nothing with the items it drops (when full or when the validator returns false) beyond dropping them; any cleanup belongs in the validator or in the caller.

Auto Trait Implementations§

§

impl<T> !Freeze for ObjectPool<T>

§

impl<T> !RefUnwindSafe for ObjectPool<T>

§

impl<T> !UnwindSafe for ObjectPool<T>

§

impl<T> Send for ObjectPool<T>
where Box<dyn Fn() -> T + Sync + Send>: Send, Option<Box<dyn Fn(&mut T) -> bool + Sync + Send>>: Send, Mutex<Vec<T>>: Send,

§

impl<T> Sync for ObjectPool<T>
where Box<dyn Fn() -> T + Sync + Send>: Sync, Option<Box<dyn Fn(&mut T) -> bool + Sync + Send>>: Sync, Mutex<Vec<T>>: Sync,

§

impl<T> Unpin for ObjectPool<T>
where Box<dyn Fn() -> T + Sync + Send>: Unpin, Option<Box<dyn Fn(&mut T) -> bool + Sync + Send>>: Unpin, Mutex<Vec<T>>: Unpin,

§

impl<T> UnsafeUnpin for ObjectPool<T>
where Box<dyn Fn() -> T + Sync + Send>: UnsafeUnpin, Option<Box<dyn Fn(&mut T) -> bool + Sync + Send>>: UnsafeUnpin, Mutex<Vec<T>>: UnsafeUnpin,

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> ClassDefaults for T
where T: ?Sized,

Source§

fn static_constructor()

The static constructor of a type: work done once per thread, after the properties of the type are registered and before its first instance exists (class handlers, affects_render, metadata overrides). 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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.