Skip to main content

Pool

Struct Pool 

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

A slotmap-style arena: values in, Handles out.

Insertion recycles free slots through a free list, so live values stay densely packed near the front of the backing Vec under steady-state churn. Every removal bumps the slot’s generation, so handles issued before the removal stop resolving.

§Generation exhaustion

Generations are 32-bit and never wrap. A slot that has been recycled u32::MAX - 1 times is retired: its storage is dropped, the slot is removed from the free list, and it is never handed out again. Wrapping would silently resurrect a >4-billion-removals-old handle as a valid alias for an unrelated value — a bug that would surface as impossible-looking aliasing weeks into a server’s uptime. Retiring instead costs one dead Vec entry (a few bytes) per exhausted slot, and only after ~4.3e9 reuses of that one slot: at 60 Hz, recycling the same slot every tick, that is over two years of continuous churn. See Pool::retired_slots to observe it.

§Safety

This module contains no unsafe. The Vec<Slot<T>> + enum payload costs one discriminant per slot versus a hand-rolled MaybeUninit arena; clarity wins at this layer, and the pool is not in any inner loop that measures.

Implementations§

Source§

impl<T> Pool<T>

Source

pub const fn new() -> Self

Creates an empty pool.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty pool with room for capacity slots.

Source

pub const fn len(&self) -> usize

Number of live values.

Source

pub const fn is_empty(&self) -> bool

Whether the pool holds no live values.

Source

pub const fn retired_slots(&self) -> usize

Number of slots permanently withdrawn by generation exhaustion.

Expected to be zero forever; a non-zero value in a long-running server is worth a metric, not an alarm.

Source

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

Inserts value and returns a handle to it.

§Panics

If the pool would exceed u32::MAX + 1 slots — the index is 32-bit by design, and a pool that large is a leak, not a workload.

Source

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

Borrows the value handle names, or None if the handle is stale.

Source

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

Mutably borrows the value handle names, or None if the handle is stale.

Source

pub fn contains(&self, handle: Handle<T>) -> bool

Whether handle still resolves.

Source

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

Removes and returns the value handle names, invalidating every handle to that slot.

Returns None (and changes nothing) if the handle is already stale.

Source

pub fn clear(&mut self)

Removes every value, invalidating all outstanding handles.

Slot storage is kept for reuse; generations continue from where they were, so handles issued before the clear stay dead.

Source

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

Iterates live (handle, &value) pairs in slot order.

Source

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

Iterates live (handle, &mut value) pairs in slot order.

Trait Implementations§

Source§

impl<T> Debug for Pool<T>

Deliberately does not print the values: a pool is usually large, and this impl must exist for every T, not just T: Debug.

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Pool<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Extend<T> for Pool<T>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T> FromIterator<T> for Pool<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more

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.