pub struct SyncRegion<T> { /* private fields */ }Expand description
A thread-safe wrapper around Region<T> — the trusted concurrent baseline.
This is a coarse-grained std::sync::RwLock<Region<T>> with an ergonomic
guard-based API: multiple readers (read) or one writer (write) at a time.
It is the always-shippable concurrent answer: correct under any interleaving
because every mutation serialises through the lock. Lock-free tiers
(Phase 3b) are a later opt-in upgrade for read-mostly hot paths; until those
land and clear loom/TSan, this is the default for shared mutable regions.
The wrapper stays #![forbid(unsafe_code)]: all interior mutability comes
from std’s RwLock. Use read / write for
multi-operation transactions (the borrows tie to the guard), or the
one-shot convenience methods (insert,
remove, …) which take &self and lock internally.
§Poisoning policy
A panic while a guard is held poisons the RwLock. A poisoned Region is
still structurally valid — no broken memory invariants: slotmap keeps the
dense store generational and consistent regardless of a panicked op, so we
recover from poison rather than propagate it. Every accessor uses
RwLockReadGuard/RwLockWriteGuard recovery (PoisonError::into_inner),
handing back the intact inner Region and letting callers continue. This
keeps a panic in one thread from bricking the region for all others.
Implementations§
Source§impl<T> SyncRegion<T>
impl<T> SyncRegion<T>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty region with space pre-reserved for capacity entries.
Sourcepub fn read(&self) -> RwLockReadGuard<'_, Region<T>>
pub fn read(&self) -> RwLockReadGuard<'_, Region<T>>
Locks for shared read, returning a guard that hands out &Region<T>.
Multiple readers may hold the guard concurrently. Recovers from poison (see the poisoning policy).
Sourcepub fn write(&self) -> RwLockWriteGuard<'_, Region<T>>
pub fn write(&self) -> RwLockWriteGuard<'_, Region<T>>
Locks for exclusive write, returning a guard that hands out &mut Region<T>.
Blocks all other readers and writers until dropped. Recovers from poison (see the poisoning policy).
Sourcepub fn insert(&self, value: T) -> Handle<T>
pub fn insert(&self, value: T) -> Handle<T>
Inserts value, returning a fresh handle that resolves to it (I1).
One-shot convenience that locks for write internally. For a transaction
that does several ops under one lock, use write instead.
Sourcepub fn remove(&self, handle: Handle<T>) -> Option<T>
pub fn remove(&self, handle: Handle<T>) -> Option<T>
Removes and returns the value for handle, or None if stale/removed.
One-shot convenience that locks for write internally.
Sourcepub fn contains(&self, handle: Handle<T>) -> bool
pub fn contains(&self, handle: Handle<T>) -> bool
Whether handle currently resolves to a live value.
One-shot convenience that locks for read internally.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of live values (I4).
One-shot convenience that locks for read internally. Note that under concurrency the count is a momentary snapshot, not a stable property.
Sourcepub fn clear(&self)
pub fn clear(&self)
Removes every value, invalidating all outstanding handles.
One-shot convenience that locks for write internally.
Sourcepub fn get_cloned(&self, handle: Handle<T>) -> Option<T>where
T: Clone,
pub fn get_cloned(&self, handle: Handle<T>) -> Option<T>where
T: Clone,
Clones the value for handle out without holding a guard, or None if
stale/removed. One-shot convenience that locks for read internally.
Prefer this over read when you only need a by-value copy
and don’t want to hold the guard across other work.