pub struct SpscRingBuffer<T> { /* private fields */ }Expand description
Zero-copy SPSC ring buffer with proper memory ordering
§Safety
This type is only safe to use when:
- Only one producer thread exists
- Only one consumer thread exists
- Memory is properly aligned for the element type T
Implementations§
Source§impl<T> SpscRingBuffer<T>
impl<T> SpscRingBuffer<T>
Sourcepub fn new(config: RingConfig) -> Self
pub fn new(config: RingConfig) -> Self
Create a new SPSC ring buffer with the given configuration
Sourcepub fn push(&self, value: T) -> Result<(), T>
pub fn push(&self, value: T) -> Result<(), T>
Push an element into the ring buffer
Returns Ok(()) on success, or Err(value) if the ring is full.
When full, the producer should wait/backpressure until space is available.
Sourcepub fn pop(&self) -> Option<T>
pub fn pop(&self) -> Option<T>
Pop an element from the ring buffer
Returns Some(value) on success, or None if the ring is empty.
Sourcepub fn overflow_count(&self) -> usize
pub fn overflow_count(&self) -> usize
Get overflow count for backpressure monitoring
Sourcepub fn empty_count(&self) -> usize
pub fn empty_count(&self) -> usize
Get empty count for backpressure monitoring
Sourcepub fn should_backpressure(&self) -> bool
pub fn should_backpressure(&self) -> bool
Check if backpressure should be applied (ring >= threshold)
Sourcepub fn write_available(&self) -> usize
pub fn write_available(&self) -> usize
Number of elements that can currently be pushed without blocking.
Sourcepub fn read_available(&self) -> usize
pub fn read_available(&self) -> usize
Number of elements currently available to pop (best-effort, not a snapshot).
Sourcepub fn wait_for_space(&self)
pub fn wait_for_space(&self)
Wait for space to become available
Uses exponential backoff: spins with PAUSE up to 256 iterations, then transitions to OS yield to avoid starving the consumer.
Sourcepub fn wait_for_data(&self)
pub fn wait_for_data(&self)
Wait for an element to become available
Uses exponential backoff: spins with PAUSE up to 256 iterations, then transitions to OS yield to avoid starving the producer.
Sourcepub fn push_batch(&self, slice: &[T]) -> usizewhere
T: Copy,
pub fn push_batch(&self, slice: &[T]) -> usizewhere
T: Copy,
Push a batch of elements, returning the number successfully pushed.
Stops early if the ring becomes full. This amortizes atomic store
overhead across multiple elements: only two atomic stores (tail + cached_head)
for up to slice.len() pushes.
Uses prefetching to reduce cache misses on write path.
Sourcepub fn pop_batch(&self, out: &mut [MaybeUninit<T>]) -> usize
pub fn pop_batch(&self, out: &mut [MaybeUninit<T>]) -> usize
Pop up to count elements into the provided mutable slice, returning
the number of elements actually popped.
Uses MaybeUninit to avoid zero-initialisation overhead.
Uses prefetching to reduce cache misses on read path.
Trait Implementations§
Source§impl<T: Debug> Debug for SpscRingBuffer<T>
impl<T: Debug> Debug for SpscRingBuffer<T>
Source§impl<T> Drop for SpscRingBuffer<T>
impl<T> Drop for SpscRingBuffer<T>
impl<T: Send> Send for SpscRingBuffer<T>
impl<T: Send> Sync for SpscRingBuffer<T>
Auto Trait Implementations§
impl<T> !Freeze for SpscRingBuffer<T>
impl<T> !RefUnwindSafe for SpscRingBuffer<T>
impl<T> Unpin for SpscRingBuffer<T>where
T: Unpin,
impl<T> UnsafeUnpin for SpscRingBuffer<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for SpscRingBuffer<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more