pub struct Region<T> { /* private fields */ }Expand description
A handle-addressed store of T.
A thin typed membrane over slotmap::SlotMap<slotmap::DefaultKey, T>:
values live in slotmap’s dense, cache-friendly, always-compact backing
store, and every operation delegates to slotmap while exposing only typed
Handle<T> values (raw DefaultKeys never escape). All operations are
O(1).
§Invariants upheld
- I1 — resolution: a fresh handle resolves via
getto the inserted value until it isremoved. - I2 — tombstone: after
remove(h),get(h)isNoneforever and a secondremove(h)is a no-opNone. - I3 — no ABA: a stale handle — one whose slot has since been reused —
never resolves to a live value.
slotmap’sDefaultKeycarries a generation that is bumped on removal, so the old handle fails the version check and yieldsNone. - I4 — accounting:
lenequals the number of live entries andis_emptyagrees. - I5 — drop-once: every live value is dropped exactly once — on
remove(returned to the caller) or onRegiondrop — never twice, never leaked.slotmapowns the storage and therefore the drops.
§Generation saturation
Version saturation (a slot whose generation would have to wrap) is
slotmap’s responsibility: DefaultKey retires such a slot rather than
wrapping a generation into alias, so a handle can never alias a future value
— the classic generational-arena ABA caveat stays closed at the
astronomically rare cost of one slot per 2^32 reuses. There is no
hand-rolled retirement code in this crate.
Implementations§
Source§impl<T> Region<T>
impl<T> Region<T>
Sourcepub fn with_capacity(capacity: usize) -> Region<T>
pub fn with_capacity(capacity: usize) -> Region<T>
Creates an empty region with space pre-reserved for capacity entries.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more insertions.
Does nothing if the backing store already has room. After a churn that
removes entries, the freed slots live on the free list, so re-inserting
reuses existing capacity and does not grow unboundedly (the backing
stays bounded by the high-water mark of live entries). Delegates to
slotmap’s reserve; may allocate more than asked to avoid frequent
reallocations. Panics if the new allocation size overflows usize.
Sourcepub fn insert(&mut self, value: T) -> Handle<T>
pub fn insert(&mut self, value: T) -> Handle<T>
Inserts value, returning a fresh handle that resolves to it (I1).
Sourcepub fn get(&self, handle: Handle<T>) -> Option<&T>
pub fn get(&self, handle: Handle<T>) -> Option<&T>
Borrows the value for handle, or None if the handle is stale or
removed (I1, I2, I3).
Sourcepub fn get_mut(&mut self, handle: Handle<T>) -> Option<&mut T>
pub fn get_mut(&mut self, handle: Handle<T>) -> Option<&mut T>
Mutably borrows the value for handle, or None if stale/removed.
Sourcepub fn contains(&self, handle: Handle<T>) -> bool
pub fn contains(&self, handle: Handle<T>) -> bool
Whether handle currently resolves to a live value.
Sourcepub fn remove(&mut self, handle: Handle<T>) -> Option<T>
pub fn remove(&mut self, handle: Handle<T>) -> Option<T>
Removes and returns the value for handle, or None if it is already
stale/removed. After this, handle resolves to None forever (I2).
Sourcepub fn iter(&self) -> impl Iterator<Item = &T>
pub fn iter(&self) -> impl Iterator<Item = &T>
Iterates the live values in dense (cache-friendly) order. The order is unspecified and changes as elements are removed.