Struct maskerad_object_pool::RcPool
[−]
[src]
pub struct RcPool<T: Recyclable>(_);
A wrapper around a vector of RcHandle<T>
.
Example
use maskerad_object_pool::RcPool; //create 20 monsters with default initialization let pool = RcPool::with_capacity(20, || { Monster::default() }); { // Get the first "non-used" monster. // Monster's default initialization set their level at 10. let a_monster = pool.create_strict()?; a_monster.borrow_mut().level_up(); assert_eq!(a_monster.borrow_mut().level, 11); //The monster is now used assert_eq!(pool.nb_unused(), 19); //After the closing brace, the handle to the monster will be //dropped. It will reinitialize the monster to a state defined by //the 'Poolable' trait. } assert_eq!(pool.nb_unused(), 20);
Methods
impl<T: Recyclable> RcPool<T>
[src]
fn with_capacity<F>(size: usize, op: F) -> Self where
F: Fn() -> T,
[src]
F: Fn() -> T,
Create an object pool with the given capacity, and instantiate the given number of object.
Example
use maskerad_object_pool::RcPool; let pool = RcPool::with_capacity(20, || { Monster::default() }); assert_eq!(pool.nb_unused(), 20);
fn pool_slice(&self) -> &[RcHandle<T>]
[src]
Returns an immutable slice of the vector of RcHandle<T>
Example
use maskerad_object_pool::RcPool; let pool = RcPool::with_capacity(10, || { Monster::default() }); //The pool slice can be useful if you need tou iterate over the collection. let nb_lvl_5_monsters = pool.pool_slice() .iter() .filter(|handle| { handle.borrow().level == 5 }) .count(); //All monsters begin at level 10, there is no monsters at level 5. assert_eq!(nb_lvl_5_monsters, 0);
fn create_strict(&self) -> PoolResult<RcHandle<T>>
[src]
Ask the pool for an RcHandle<T>
, returning a PoolResult<RcHandle<T>>
. If you cannot increase the pool size because of
memory restrictions, this function may be more convenient than the "non-strict" one.
Errors
If all RcHandle<T>
are used, a PoolError is returned indicating that all RcHandle<T>
are used.
Example
use maskerad_object_pool::RcPool; let pool = RcPool::with_capacity(1, || { Monster::default() }); let a_monster = pool.create_strict()?; assert!(pool.create_strict().is_err());
fn create(&self) -> Option<RcHandle<T>>
[src]
Asks the pool for an RcHandle<T>
, returning an Option<RcHandle<T>>
.
Example
use maskerad_object_pool::RcPool; let pool = RcPool::with_capacity(1, || { Monster::default() }); let a_monster = pool.create(); assert!(a_monster.is_some()); assert!(pool.create().is_none()); match pool.create() { Some(monster) => println!("will not happen."), None => { // do something, or nothing. }, }
fn nb_unused(&self) -> usize
[src]
Return the number of non-used RcHandle<T>
in the pool.
Example
use maskerad_object_pool::RcPool; let pool = RcPool::with_capacity(2, || { Monster::default() }); assert_eq!(pool.nb_unused(), 2); let a_monster = pool.create(); assert!(a_monster.is_some()); assert_eq!(pool.nb_unused(), 1);
fn capacity(&self) -> usize
[src]
Returns the maximum capacity of the vector of RcHandle<T>
.
Example
use maskerad_object_pool::RcPool; let pool = RcPool::with_capacity(2, || { Monster::default() }); assert_eq!(pool.capacity(), 2);