Skip to main content

Handle

Struct Handle 

Source
pub struct Handle<T, G: GenerationInt = u32> { /* private fields */ }
Expand description

Stable, non-pointer handle to a slot in a GenerationalSlab.

Copy (cheap to pass), Eq + Hash (works in maps), and notably does NOT carry a reference to the slab — handles do not extend the slab’s lifetime. A handle outliving its slab is allowed and gives None on access.

§Generation wraparound — closed by slot retirement

The generation counter increments on every remove of a slot and is wrapping_add(1) (per GenerationInt). Without mitigation, after 2^G reuses of the same slot the counter would return to its original value and a stale Handle could be re-accepted against a different T (the classic ABA problem at a long horizon).

This is defended, not merely documented: when a slot’s generation wraps a full cycle (the increment reaches ZERO), remove retires the slot — it is never returned to the freelist, so it can never be re-issued at a generation a stale handle holds. The cost is one leaked slot per full wrap of that slot (4.3 billion removes for u32, which is negligible). The choice between u32 and u64 is therefore now about how soon a hot slot is retired, not about a UAF window:

  • G = u32 (default): wrap after 2^32 ≈ 4.3 billion reuses of the same slot. Realistic for long-running servers with high churn on a small slab (e.g. a 1024-slot connection pool processing millions of connections per second).
  • G = u64: wrap after 2^64 reuses — effectively unreachable in any realistic deployment (>500 years at 1 GHz of pure slot churn). Recommended for long-lived servers; the per-handle cost is 8 extra bytes (8 bytes for u32, 16 for u64 — the wider counter forces 8-byte struct alignment and 4 bytes of tail padding).

Copy means a handle can outlive the slot’s original lifetime arbitrarily — including past 2^G recycles. If your handles can realistically be stashed for that long (audit logs, persisted session records, snapshot indices), use Handle<T, u64>. For per-request handles that never escape the request scope, u32 is fine.

§Known limitation — cross-pool handle confusion

Handle<T, G> is typed by T (and G) but not by which GenerationalSlab<T, B, G> issued it. Passing a handle returned by pool_a.insert(...) to pool_b.get(...) is currently a runtime concern, not a compile error: the slot index will be interpreted against pool_b’s slot array, and ABA-safety degenerates to “match-by-coincidence” on the generation counter.

Closing this gap requires runtime-unique branding — either an invariant-lifetime tag (generativity crate style) or a monotonic pool-id passed through PhantomData — and is API-breaking because every Handle<T, G> user signature would gain an extra type parameter. The v0.1 API ships without it; v2.0+ may revisit. See the “Generational-handle slab” recipe in docs/COMPOSITION_RECIPES.md for the documented pitfall.

Until branded, callers who keep multiple pools of the same T must NOT mix their handles. The naming convention recommended in the recipes doc is to type-alias each pool’s handle: type SessionHandle = Handle<Session, u32> per pool, in different modules.

Trait Implementations§

Source§

impl<T: Clone, G: Clone + GenerationInt> Clone for Handle<T, G>

Source§

fn clone(&self) -> Handle<T, G>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Copy, G: Copy + GenerationInt> Copy for Handle<T, G>

Source§

impl<T: Debug, G: Debug + GenerationInt> Debug for Handle<T, G>

Source§

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

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

impl<T: Eq, G: Eq + GenerationInt> Eq for Handle<T, G>

Source§

impl<T: Hash, G: Hash + GenerationInt> Hash for Handle<T, G>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: PartialEq, G: PartialEq + GenerationInt> PartialEq for Handle<T, G>

Source§

fn eq(&self, other: &Handle<T, G>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, G: GenerationInt> StructuralPartialEq for Handle<T, G>

Auto Trait Implementations§

§

impl<T, G = u32> !Send for Handle<T, G>

§

impl<T, G = u32> !Sync for Handle<T, G>

§

impl<T, G> Freeze for Handle<T, G>
where G: Freeze,

§

impl<T, G> RefUnwindSafe for Handle<T, G>

§

impl<T, G> Unpin for Handle<T, G>
where G: Unpin,

§

impl<T, G> UnsafeUnpin for Handle<T, G>
where G: UnsafeUnpin,

§

impl<T, G> UnwindSafe for Handle<T, G>

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.