Skip to main content

SpscRing

Struct SpscRing 

Source
pub struct SpscRing<T, const CAP: usize> { /* private fields */ }
Expand description

Lock-free single-producer/single-consumer ring, fixed const capacity.

The design is the Vyukov bounded queue: every slot carries a sequence number, the cursors advance monotonically and index slots by mask (CAP must be a power of two, checked at compile time), and the two cursors sit on their own cache lines (repr(align(64))) so producer and consumer never false-share. The slot buffer is allocated exactly once in SpscRing::new; try_push, try_pop, and len never allocate or panic.

§The sanctioned unsafe exception

This is the estate’s approved use of raw slot access: UnsafeCell slots behind per-slot sequence atomics. The handoff edge is the standard acquire/release message-passing pair, zero SeqCst (model-checked under loom in tests/loom_spsc.rs):

  • Producer: reads its cursor Relaxed, loads the slot sequence with Acquire (empty slot confirmed), writes the payload, stores pos + 1 with Release (publish), then advances its own cursor Relaxed.
  • Consumer: mirror image — Acquire load of the sequence confirms a published value, takes the payload, stores pos + CAP with Release (slot reusable one lap later), advances its cursor Relaxed.

§Single-producer / single-consumer contract

try_push is the producer side, try_pop the consumer side: at most one thread may push and at most one may pop concurrently. Concurrent try_pushes or try_pops are a contract violation, not dynamically enforced — the documented cost of the sanctioned exception. Both operations are lock-free: neither side ever blocks; a full ring hands the value back, an empty ring hands back None, and the caller retries.

Implementations§

Source§

impl<T, const CAP: usize> SpscRing<T, CAP>

Source

pub fn new() -> Self

New ring with CAP slots. Compile-time refusal of non-power-of-two capacities (the mask indexing requires one) and of CAP < 2: with a single slot the sequence protocol’s two transitions (pos + 1 on write, pos + CAP on consume) collide on the same number, and the producer would overwrite an unconsumed value (a lost update that a lenient loom assertion cannot catch — the model found it). The only allocation in the ring’s lifetime happens here.

Source

pub fn try_push(&self, value: T) -> Option<T>

Producer side: push one value, or hand it straight back when the ring is full. Lock-free, never blocks, never allocates, never panics. The returned Some(value) is unchanged and not stored.

Source

pub fn try_pop(&self) -> Option<T>

Consumer side: pop the oldest value, or None when empty. Lock-free, never blocks, never allocates, never panics.

Source

pub fn len(&self) -> usize

Approximate occupancy. Relaxed loads, so the pair may lag concurrent progress; a hint, never a correctness input.

Source

pub fn is_empty(&self) -> bool

True when the ring appears empty (see len).

Trait Implementations§

Source§

impl<T, const CAP: usize> Debug for SpscRing<T, CAP>

Source§

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

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

impl<T, const CAP: usize> Default for SpscRing<T, CAP>

Source§

fn default() -> Self

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

impl<T: Send, const CAP: usize> Send for SpscRing<T, CAP>

Source§

impl<T: Send, const CAP: usize> Sync for SpscRing<T, CAP>

Auto Trait Implementations§

§

impl<T, const CAP: usize> !Freeze for SpscRing<T, CAP>

§

impl<T, const CAP: usize> !RefUnwindSafe for SpscRing<T, CAP>

§

impl<T, const CAP: usize> Forget for SpscRing<T, CAP>

§

impl<T, const CAP: usize> Move for SpscRing<T, CAP>

§

impl<T, const CAP: usize> Unpin for SpscRing<T, CAP>
where Align8<usize>: Unpin, Box<[Slot<T>]>: Unpin,

§

impl<T, const CAP: usize> UnsafeUnpin for SpscRing<T, CAP>
where Align8<usize>: UnsafeUnpin, *const [Slot<T>] is !null: UnsafeUnpin, PhantomData<[Slot<T>]>: UnsafeUnpin,

§

impl<T, const CAP: usize> UnwindSafe for SpscRing<T, CAP>
where Align8<usize>: UnwindSafe, BoxRaw<[Slot<T>]>: UnwindSafe,

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

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.