Skip to main content

Pool

Struct Pool 

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

A fixed-capacity slot pool handing out generation-checked handles.

Implementations§

Source§

impl<T> Pool<T>

Source

pub fn with_capacity(capacity: usize) -> Self

Reserve room for capacity objects. The pool never allocates again: it hands out None when full rather than growing.

Source

pub fn capacity(&self) -> usize

Slots the pool reserved.

Source

pub fn len(&self) -> usize

Live objects.

Source

pub fn is_empty(&self) -> bool

Whether the pool holds no live objects.

Source

pub fn reserved_bytes(&self) -> u64

Bytes the pool reserved, occupied or not: what it costs the process whatever its occupancy, and so what it reports to a byte budget.

Source

pub fn insert(&mut self, value: T) -> Option<PoolHandle>

Place value in a free slot. None when the pool is full, which is the caller’s cue to drop the request or widen the pool at setup.

Source

pub fn remove(&mut self, handle: PoolHandle) -> Option<T>

Take the object back out, freeing its slot. None when the handle is stale or already removed.

Source

pub fn get(&self, handle: PoolHandle) -> Option<&T>

Borrow the object a handle names, if the handle is still live.

Source

pub fn get_mut(&mut self, handle: PoolHandle) -> Option<&mut T>

Mutably borrow the object a handle names, if it is still live.

Source

pub fn get_at(&self, index: usize) -> Option<&T>

Borrow whatever occupies a slot, by position rather than by handle.

For a caller that keeps its own tables alongside the pool and indexes them by slot: the position came from the pool, so re-checking a generation it never left would only cost a branch.

Source

pub fn get_at_mut(&mut self, index: usize) -> Option<&mut T>

Mutably borrow whatever occupies a slot, by position.

Source

pub fn handle_at(&self, index: usize) -> Option<PoolHandle>

The handle naming whatever occupies a slot, so a caller that walks positions can hand one back out. None when the slot is vacant.

Source

pub fn contains(&self, handle: PoolHandle) -> bool

Whether a handle still names a live object.

Source

pub fn iter(&self) -> impl Iterator<Item = (PoolHandle, &T)>

Every live object with its handle, in slot order.

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (PoolHandle, &mut T)>

Every live object with its handle, mutably, in slot order.

Source

pub fn clear(&mut self)

Drop every occupant, keeping the reserved storage. Outstanding handles go stale, as they would if each object were removed individually.

Auto Trait Implementations§

§

impl<T> Freeze for Pool<T>
where Vec<Slot<T>>: Freeze,

§

impl<T> RefUnwindSafe for Pool<T>
where Vec<Slot<T>>: RefUnwindSafe,

§

impl<T> Send for Pool<T>
where Vec<Slot<T>>: Send,

§

impl<T> Sync for Pool<T>
where Vec<Slot<T>>: Sync,

§

impl<T> Unpin for Pool<T>
where Vec<Slot<T>>: Unpin,

§

impl<T> UnsafeUnpin for Pool<T>
where Vec<Slot<T>>: UnsafeUnpin,

§

impl<T> UnwindSafe for Pool<T>
where Vec<Slot<T>>: UnwindSafe,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.