pub struct SpscQueue<T> { /* private fields */ }Expand description
Cache-aligned, lock-free single-producer/single-consumer ring buffer.
Fixed power-of-two capacity. See the module-level comment above for
why head/tail are each cache-line-aligned separately instead of
aligning the whole struct.
Implementations§
Source§impl<T> SpscQueue<T>
impl<T> SpscQueue<T>
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Build an empty queue. capacity must be a power of two.
§Panics
Panics if capacity is not a power of two.
Sourcepub fn push(&self, value: T) -> Result<(), T>
pub fn push(&self, value: T) -> Result<(), T>
Single-producer push. Returns Err(value) (handing the value back)
if the queue is full — never blocks.
§Errors
Returns Err(value), handing the value straight back to the
caller, if the ring buffer is currently full (tail - head has
reached capacity). This is not a fault — it is the normal
backpressure signal for a bounded SPSC queue; the caller decides
whether to retry, drop the value, or apply its own backoff.
Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Whether the queue is currently at capacity (the next Self::push
would be rejected).
Source§impl<T: Copy> SpscQueue<T>
impl<T: Copy> SpscQueue<T>
Sourcepub fn push_slice(&self, src: &[T]) -> usize
pub fn push_slice(&self, src: &[T]) -> usize
Single-producer bulk push: copy as many elements from src as fit
into the ring, returning the count actually pushed (0 if full).
This is the bulk analogue of push and exists purely
as a hot-path optimization for T: Copy element types (e.g. the
byte pipe in stream::native): pushing a run of N bytes one
push call at a time pays N separate atomic
load/store pairs and N bounds-checked writes, whereas this issues at
most two copy_nonoverlapping runs (the ring may wrap once) and a
single tail store. Behaviour is otherwise identical — same
capacity accounting, same Release publication of the new tail.