Skip to main content

SyncRegion

Struct SyncRegion 

Source
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>

Source

pub fn new() -> SyncRegion<T>

Creates an empty region that allocates nothing until first use.

Source

pub fn with_capacity(capacity: usize) -> SyncRegion<T>

Creates an empty region with space pre-reserved for capacity entries.

Source

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).

Source

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).

Source

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.

Source

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.

Source

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

Whether handle currently resolves to a live value.

One-shot convenience that locks for read internally.

Source

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.

Source

pub fn is_empty(&self) -> bool

Whether the region holds no live values (I4).

Source

pub fn clear(&self)

Removes every value, invalidating all outstanding handles.

One-shot convenience that locks for write internally.

Source

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.

Trait Implementations§

Source§

impl<T> Default for SyncRegion<T>

Source§

fn default() -> SyncRegion<T>

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

Auto Trait Implementations§

§

impl<T> !Freeze for SyncRegion<T>

§

impl<T> RefUnwindSafe for SyncRegion<T>

§

impl<T> Send for SyncRegion<T>
where T: Send,

§

impl<T> Sync for SyncRegion<T>
where T: Send + Sync,

§

impl<T> Unpin for SyncRegion<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for SyncRegion<T>

§

impl<T> UnwindSafe for SyncRegion<T>

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 = 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.