[][src]Struct refpool::Pool

pub struct Pool<A> { /* fields omitted */ }

A pool of preallocated memory sized to match type A.

In order to use it to allocate objects, pass it to PoolRef::new() or PoolRef::default().

Example

let mut pool: Pool<usize> = Pool::new(1024);
let pool_ref = PoolRef::new(&mut pool, 31337);
assert_eq!(31337, *pool_ref);

Methods

impl<A> Pool<A>[src]

pub fn new(max_size: usize) -> Self[src]

Construct a new pool with a given max size and return a handle to it.

Values constructed via the pool will be returned to the pool when dropped, up to max_size. When the pool is full, values will be dropped in the regular way.

If max_size is 0, meaning the pool can never hold any dropped values, this method will give you back a null handle without allocating a pool. You can still use this to construct PoolRef values, they'll just allocate in the old fashioned way without using a pool. It is therefore advisable to use a zero size pool as a null value instead of Option<Pool>, which eliminates the need for unwrapping the Option value.

pub fn get_max_size(&self) -> usize[src]

Get the maximum size of the pool.

pub fn get_pool_size(&self) -> usize[src]

Get the current size of the pool.

pub fn is_full(&self) -> bool[src]

Test if the pool is currently full.

pub fn fill(&self)[src]

Fill the pool with empty allocations.

This operation will pre-allocate self.get_max_size() - self.get_pool_size() memory chunks, without initialisation, and put them in the pool.

Examples

let pool: Pool<usize> = Pool::new(1024);
assert_eq!(0, pool.get_pool_size());
pool.fill();
assert_eq!(1024, pool.get_pool_size());

pub fn cast<B>(&self) -> Pool<B>[src]

Convert a pool handle for type A into a handle for type B.

The types A and B must have the same size and alignment, as per std::mem::size_of and std::mem::align_of, or this method will panic.

This lets you use the same pool to construct values of different types, as long as they are of the same size and alignment, so they can reuse each others' memory allocations.

Examples

let u64_pool: Pool<u64> = Pool::new(1024);
let u64_number = PoolRef::new(&u64_pool, 1337);

let i64_pool: Pool<i64> = u64_pool.cast();
let i64_number = PoolRef::new(&i64_pool, -1337);

Trait Implementations

impl<A> Clone for Pool<A>[src]

impl<A> Debug for Pool<A>[src]

fn fmt(&self, f: &mut Formatter) -> Result<(), Error>[src]

Debug implementation for Pool.

Examples

let mut pool: Pool<usize> = Pool::new(256);
assert!(format!("{:?}", pool).starts_with("Pool[0/256]:0x"));
pool.fill();
assert!(format!("{:?}", pool).starts_with("Pool[256/256]:0x"));

impl<A> Drop for Pool<A>[src]

Auto Trait Implementations

impl<A> RefUnwindSafe for Pool<A> where
    A: RefUnwindSafe

impl<A> !Send for Pool<A>

impl<A> !Sync for Pool<A>

impl<A> Unpin for Pool<A>

impl<A> UnwindSafe for Pool<A> where
    A: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.