Struct Pool

Source
pub struct Pool<A> { /* private fields */ }
Expand description

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

Implementations§

Source§

impl<A> Pool<A>

Source

pub fn new(max_size: usize) -> Self

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.

Source

pub fn get_max_size(&self) -> usize

Get the maximum size of the pool.

Source

pub fn get_pool_size(&self) -> usize

Get the current size of the pool.

Source

pub fn is_full(&self) -> bool

Test if the pool is currently full.

Source

pub fn fill(&self)

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

pub fn filled(self) -> Self

Fill the pool and return it.

This is a convenience function that calls fill() on the pool, so that you can construct a pool with a one liner:

let pool: Pool<u64> = Pool::new(1024).filled();
assert!(pool.is_full());

This is functionally equivalent to, but terser than:

let mut pool: Pool<u64> = Pool::new(1024);
pool.fill();
assert!(pool.is_full());
Source

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

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§

Source§

impl<A> Clone for Pool<A>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A> Debug for Pool<A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

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"));
Source§

impl<A> Drop for Pool<A>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<A> Freeze for Pool<A>

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.