Skip to main content

SpscRing

Struct SpscRing 

Source
pub struct SpscRing<T> { /* private fields */ }
Expand description

Lock-free single-producer/single-consumer ring with fixed capacity.

The slot buffer is allocated exactly once in SpscRing::new; push, pop, and len never allocate or panic. Indices are monotonic u64s — the modern technique that needs no power-of-two capacity: slot = index % capacity, empty when head == tail, full when tail - head == capacity. u64 indices cannot wrap within the lifetime of any process (2^64 single ops at 1 GHz is ~584 years), and wrapping_* keeps the arithmetic panic-free regardless.

§The sanctioned unsafe exception

This is the estate’s approved use of raw slot access: a MaybeUninit buffer behind UnsafeCell, synchronized by two atomics. The protocol (model-checked under loom in tests/loom_spsc.rs):

  • Producer owns tail: reads it Relaxed (own index), writes the slot, then stores tail + 1 with Release.
  • Consumer owns head: reads it Relaxed (own index), loads tail with Acquire — pairing with the producer’s Release store so the slot write is visible before the read — then stores head + 1 with Release.
  • Slot reuse: before overwriting a slot the producer loads head with Acquire, pairing with the consumer’s Release store of head, so the consumer’s earlier reads of that slot are complete.

Each slot is written at exactly one monotonic index and read at exactly one monotonic index, so the acquire/release pairs above are the only cross-thread ordering the protocol needs.

§Single-producer / single-consumer contract

push is the producer side, pop the consumer side: at most one thread may push and at most one thread may pop concurrently. Concurrent pushes or concurrent pops are a contract violation, not dynamically enforced — this is the documented cost of the sanctioned exception. Under contention either side may transiently observe a stale peer index and report full/empty one step late; the caller retries.

Implementations§

Source§

impl<T> SpscRing<T>

Source

pub fn new(capacity: usize) -> Option<Self>

New ring with capacity slots; None for capacity 0 (a ring with no slots can never hand anything off, so it is refused at construction rather than made a permanent no-op). The only allocation in the ring’s lifetime happens here.

Source

pub fn push(&self, value: T) -> Result<(), T>

Producer side: push one value. Err(value) when the ring is full (transiently possible under contention — retry). Never allocates, never panics.

§Errors

Returns Err(value) unchanged when the ring is full; the value is not stored and the caller may retry or take it back.

Source

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

Consumer side: pop the oldest value, or None when empty (transiently possible under contention — retry). Never allocates, never panics.

Source

pub fn len(&self) -> usize

Values currently in the ring (atomic snapshot: head is loaded before tail, so the difference cannot underflow; the pair may lag concurrent progress but never over-reports).

Source

pub fn is_empty(&self) -> bool

True when the ring holds no values (atomic snapshot, see [len]).

Trait Implementations§

Source§

impl<T> Debug for SpscRing<T>

Source§

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

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

impl<T> Drop for SpscRing<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: Send> Send for SpscRing<T>

Source§

impl<T: Send> Sync for SpscRing<T>

Auto Trait Implementations§

§

impl<T> !Freeze for SpscRing<T>

§

impl<T> !RefUnwindSafe for SpscRing<T>

§

impl<T> Forget for SpscRing<T>

§

impl<T> Move for SpscRing<T>

§

impl<T> Unpin for SpscRing<T>
where T: Unpin, *const u8 is !null: Unpin, Align8<u64>: Unpin,

§

impl<T> UnsafeUnpin for SpscRing<T>

§

impl<T> UnwindSafe for SpscRing<T>
where RawVecInner: UnwindSafe, T: UnwindSafe, Align8<u64>: 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.