Skip to main content

SpscQueue

Struct SpscQueue 

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

Source

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.

Source

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.

Source

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

Single-consumer pop. Returns None if the queue is empty.

Source

pub fn is_empty(&self) -> bool

Whether the queue currently has no elements.

Source

pub fn is_full(&self) -> bool

Whether the queue is currently at capacity (the next Self::push would be rejected).

Source

pub fn len(&self) -> usize

Number of elements currently queued.

Source

pub const fn capacity(&self) -> usize

The fixed capacity this queue was constructed with.

Source§

impl<T: Copy> SpscQueue<T>

Source

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.

Source

pub fn pop_slice(&self, dst: &mut [T]) -> usize

Single-consumer bulk pop: copy up to dst.len() queued elements into dst, returning the count actually popped (0 if empty).

Bulk analogue of pop; see push_slice for the rationale.

Trait Implementations§

Source§

impl<T> Drop for SpscQueue<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 SpscQueue<T>

Source§

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

Auto Trait Implementations§

§

impl<T> !Freeze for SpscQueue<T>

§

impl<T> RefUnwindSafe for SpscQueue<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for SpscQueue<T>

§

impl<T> UnsafeUnpin for SpscQueue<T>

§

impl<T> UnwindSafe for SpscQueue<T>
where 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 = 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.