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>
impl<T> ObjectPool<T>
Sourcepub const DEFAULT_MAX_SIZE: i32 = 32
pub const DEFAULT_MAX_SIZE: i32 = 32
The default of the maximum number of objects to keep in the pool.
Sourcepub fn new(
factory: impl Fn() -> T + Send + Sync + 'static,
validator: Option<Box<dyn Fn(&mut T) -> bool + Send + Sync>>,
max_size: i32,
) -> Self
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.
Sourcepub fn rent(&self) -> T
pub fn rent(&self) -> T
Rents an object from the pool or creates a new one if the pool is empty.
Sourcepub fn return_item(&self, item: T)
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>
impl<T> Sync for ObjectPool<T>
impl<T> Unpin for ObjectPool<T>
impl<T> UnsafeUnpin for ObjectPool<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
Source§impl<T> ClassDefaults for Twhere
T: ?Sized,
impl<T> ClassDefaults for Twhere
T: ?Sized,
Source§fn static_constructor()
fn static_constructor()
affects_render, metadata
overrides). Read more