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]

[src]

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

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

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

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

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

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

Trait Implementations

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

[src]

Formats the value using the given formatter.

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

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more