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]

[src]

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);

[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);

[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());

[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.
    },
}

[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);

[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);

Trait Implementations

impl<T: Debug + Recyclable> Debug for RcPool<T>
[src]

[src]

Formats the value using the given formatter.

impl<T: Clone + Recyclable> Clone for RcPool<T>
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more