Struct maskerad_object_pool::ArcPool
[−]
[src]
pub struct ArcPool<T: Recyclable>(_);
A wrapper around a vector of ArcHandle<T>
.
Example
use maskerad_object_pool::ArcPool; //create 20 monsters with default initialization let pool = ArcPool::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.write().unwrap().level_up(); assert_eq!(a_monster.read().unwrap().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> ArcPool<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::ArcPool; let pool = ArcPool::with_capacity(20, || { Monster::default() }); assert_eq!(pool.nb_unused(), 20);
fn pool_slice(&self) -> &[ArcHandle<T>]
[src]
Returns an immutable slice of the vector of ArcHandle<T>
Example
use maskerad_object_pool::ArcPool; let pool = ArcPool::with_capacity(20, || { Monster::default() }); let nb_lvl_6_monsters = pool.pool_slice() .iter() .filter(|handle| { handle.read().unwrap().level == 6 }) .count(); //All monsters start at level 10, there is no monsters at level 6. assert_eq!(nb_lvl_6_monsters, 0);
fn create_strict(&self) -> PoolResult<ArcHandle<T>>
[src]
Ask the pool for an ArcHandle<T>
, returning a PoolResult<ArcHandle<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 ArcHandle<T>
are used, a PoolError is returned indicating that all ArcHandle<T>
are used.
Example
use maskerad_object_pool::ArcPool; let pool = ArcPool::with_capacity(1, || { Monster::default() }); let a_monster = pool.create_strict()?; assert!(pool.create_strict().is_err());
fn create(&self) -> Option<ArcHandle<T>>
[src]
Asks the pool for an ArcHandle<T>
, returning an Option<ArcHandle<T>>
.
Example
use maskerad_object_pool::ArcPool; let pool = ArcPool::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 ArcHandle<T>
in the pool.
Example
use maskerad_object_pool::ArcPool; let pool = ArcPool::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 ArcHandle<T>
.
Example
use maskerad_object_pool::ArcPool; let pool = ArcPool::with_capacity(2, || { Monster::default() }); assert_eq!(pool.capacity(), 2);