pub struct Key(/* private fields */);Expand description
Opaque handle to an allocated slot.
A Key is simply an index into the slab. It does not contain a generation
counter or any other validation mechanism.
§Design Rationale: No Generational Indices
This slab intentionally omits generational indices (ABA protection). Why?
The slab is dumb storage, not a source of truth.
In real systems, your data has authoritative external identifiers:
- Exchange order IDs in trading systems
- Database primary keys in web services
- Session tokens in connection managers
When you receive a message referencing an entity, you must validate against the authoritative identifier anyway:
fn on_fill(fill: Fill, key: Key) {
let Some(order) = slab.get(key) else { return };
// This check is REQUIRED regardless of generational indices
if order.exchange_id != fill.exchange_id {
panic!("order mismatch");
}
// Process...
}Generational indices would catch the same bug that domain validation catches, but at a cost of ~8 cycles per operation. Since domain validation is unavoidable, generations provide no additional safety—only overhead.
If a stale key reaches the slab, your architecture has a bug. The fix is to correct the architecture (clear ownership, proper state machines), not to add runtime checks that mask the underlying problem.
§Sentinel
Key::NONE represents an invalid/absent key, useful for optional key
fields without Option overhead.
Implementations§
Source§impl Key
impl Key
Sourcepub const NONE: Self
pub const NONE: Self
Sentinel value representing no key / invalid key.
Equivalent to SLOT_NONE. Check with is_none.
Sourcepub const fn index(self) -> u32
pub const fn index(self) -> u32
Returns the slot index.
For bounded slabs, this is the direct slot index. For unbounded slabs, this encodes chunk and local index via power-of-2 arithmetic.