Struct Pool

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

A pool of objects that can be reused. This is useful for objects that are expensive to create, but are used frequently. This struct can be created using the Pool::new function. However, it is highly recommended that you use the ObjectPool trait instead, as it is much easier to use.

§Example

Example without deriving ObjectPool:

use derivable_object_pool::prelude::*;

#[derive(Default)]
struct Test;

static POOL: Pool<Test> = Pool::new(Test::default);

impl ObjectPool for Test {
    fn pool<'a>() -> &'a Pool<Self> {
        &POOL
    }
}

fn main() {
   let obj = Test::new();
   drop(obj); // obj is returned to the pool
   assert_eq!(POOL.len(), 1);
}

Implementations§

Source§

impl<T> Pool<T>

Source

pub const fn new(generator: fn() -> T) -> Self

Creates a new pool of objects. The pool will use the specified generator function to create new objects.

Source

pub fn len(&self) -> usize

Returns the number of objects in the pool.

Source

pub fn is_empty(&self) -> bool

Returns true if the pool is empty.

Source

pub fn insert(&self, item: T)

Inserts an object into the pool while taking ownership of it.

Source

pub fn clear(&self)

Removes all objects from the pool.

Source

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

Removes an object from the pool and returns the object while taking ownership of it.

Source§

impl<T: ObjectPool> Pool<T>

Source

pub fn remove_reusable(&self) -> Option<Reusable<T>>

Removes an object from the pool and returns a resuable wrapper for it, which will return the object to the pool when it is dropped.

Auto Trait Implementations§

§

impl<T> !Freeze for Pool<T>

§

impl<T> RefUnwindSafe for Pool<T>

§

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

§

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

§

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

§

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.