Struct maskerad_object_pool::ObjectPool
[−]
[src]
pub struct ObjectPool<T: Default>(_);
A wrapper around a vector of Handler.
Example
use maskerad_object_pool::ObjectPool; struct Monster { hp :u32, pub level: u32, } impl Default for Monster { fn default() -> Self { Monster { hp: 1, level: 1, } } } impl Monster { pub fn level_up(&mut self) { self.level += 1; } } //create 20 monsters with default initialization let pool: ObjectPool<Monster> = ObjectPool::with_capacity(20); { //Get the first "non-used" monster. let a_monster = pool.create().unwrap(); a_monster.borrow_mut().level_up(); assert_eq!(a_monster.borrow_mut().level, 2); //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 its default configuration //with the Default trait, and set it back to the non-used state. } assert_eq!(pool.nb_unused(), 20); //The object pool is just wrapper around a vector, you can use vector-related functions. assert!(pool.iter().find(|monster| { monster.borrow_mut().level == 2 } ).is_none());
Methods
impl<T: Default> RefCountedObjectPool<T>
[src]
fn with_capacity(size: usize) -> Self
[src]
Create an object pool with the given capacity, and instantiate the given number of object with their default initialization (Default trait).
Example
use maskerad_object_pool::ObjectPool; let pool: ObjectPool<String> = ObjectPool::with_capacity(20); assert_eq!(pool.nb_unused(), 20);
fn create_strict(&self) -> PoolResult<PoolObjectHandler<T>>
[src]
Ask the pool for an object, returning a Result. 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 objects are used, a PoolError is returned indicating that all objects are used.
Example
use maskerad_object_pool::ObjectPool; let pool: ObjectPool<String> = ObjectPool::with_capacity(1); let a_string = pool.create_strict().unwrap(); assert!(pool.create_strict().is_err());
fn create(&self) -> Option<PoolObjectHandler<T>>
[src]
Asks the pool for an object, returning an Option.
Error
if None is returned, you might want to:
use force_create_with_filter to reinitialize an used object, according to a predicat.
do nothing.
panic.
Example
use maskerad_object_pool::ObjectPool; let pool: ObjectPool<String> = ObjectPool::with_capacity(1); let a_string = pool.create().unwrap(); assert!(pool.create().is_none()); match pool.create() { Some(string) => println!("will not happen."), None => { // do something, or nothing. }, }
fn force_create_with_filter<P>(
&self,
predicate: P
) -> Option<PoolObjectHandler<T>> where
P: FnMut(&'r &PoolObjectHandler<T>) -> bool,
[src]
&self,
predicate: P
) -> Option<PoolObjectHandler<T>> where
P: FnMut(&'r &PoolObjectHandler<T>) -> bool,
Ask to pool to reinitialize an used object according to a predicat, returning an Option.
Error
This function will return None if the pool could not find an object matching this predicat.
Warning
Be careful with this function, you will reinitialize an used object, but the handler to this object is still there and might mutate the object.
Example
use maskerad_object_pool::ObjectPool; let pool: ObjectPool<String> = ObjectPool::with_capacity(1); let a_string = pool.create().unwrap(); a_string.borrow_mut().push_str("I'm an used string !"); assert!(pool.create().is_none()); let a_new_string = pool.force_create_with_filter(|the_contained_string| { the_contained_string.borrow_mut().contains("string") }); assert!(a_new_string.is_some()); let maybe_another_string = pool.force_create_with_filter(|the_contained_string| { the_contained_string.borrow_mut().contains("strong") }); assert!(maybe_another_string.is_none());
fn nb_unused(&self) -> usize
[src]
Return the number of non-used objects in the pool.
Example
use maskerad_object_pool::ObjectPool; let pool: ObjectPool<String> = ObjectPool::with_capacity(2); assert_eq!(pool.nb_unused(), 2); let a_string = pool.create().unwrap(); assert_eq!(pool.nb_unused(), 1);
Methods from Deref<Target = Vec<PoolObjectHandler<T>>>
fn capacity(&self) -> usize
1.0.0[src]
Returns the number of elements the vector can hold without reallocating.
Examples
let vec: Vec<i32> = Vec::with_capacity(10); assert_eq!(vec.capacity(), 10);
fn into_boxed_slice(self) -> Box<[T]>
1.0.0[src]
Converts the vector into Box<[T]>
.
Note that this will drop any excess capacity. Calling this and
converting back to a vector with into_vec
is equivalent to calling
shrink_to_fit
.
Examples
let v = vec![1, 2, 3]; let slice = v.into_boxed_slice();
Any excess capacity is removed:
let mut vec = Vec::with_capacity(10); vec.extend([1, 2, 3].iter().cloned()); assert_eq!(vec.capacity(), 10); let slice = vec.into_boxed_slice(); assert_eq!(slice.into_vec().capacity(), 3);
fn as_slice(&self) -> &[T]
1.7.0[src]
Extracts a slice containing the entire vector.
Equivalent to &s[..]
.
Examples
use std::io::{self, Write}; let buffer = vec![1, 2, 3, 5, 8]; io::sink().write(buffer.as_slice()).unwrap();
fn len(&self) -> usize
1.0.0[src]
Returns the number of elements in the vector, also referred to as its 'length'.
Examples
let a = vec![1, 2, 3]; assert_eq!(a.len(), 3);
fn is_empty(&self) -> bool
1.0.0[src]
Returns true
if the vector contains no elements.
Examples
let mut v = Vec::new(); assert!(v.is_empty()); v.push(1); assert!(!v.is_empty());
Trait Implementations
impl<T: Default> Deref for RefCountedObjectPool<T>
[src]
type Target = Vec<PoolObjectHandler<T>>
The resulting type after dereferencing.
fn deref(&self) -> &Vec<PoolObjectHandler<T>>
[src]
Dereferences the value.