pub struct RingBuffer<T> { /* private fields */ }Expand description
A lock-free ring buffer with runtime-specified capacity.
This buffer supports both SPSC and MPSC modes:
- SPSC: Single producer uses
push, single consumer usespop - MPSC: Multiple producers use
claim_and_write, single consumer usespop
§Safety
The buffer is safe to use from multiple threads as long as:
- In SPSC mode: Exactly one producer and one consumer thread
- In MPSC mode: Any number of producers, exactly one consumer thread
Implementations§
Source§impl<T> RingBuffer<T>
impl<T> RingBuffer<T>
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Creates a new ring buffer with the given capacity.
The capacity will be clamped to [MIN_BUFFER_SIZE, MAX_BUFFER_SIZE]
and rounded up to the next power of 2 for efficiency.
§Panics
Panics if capacity is 0.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the buffer is empty.
Note: This is a snapshot and may change immediately after returning. Uses Relaxed ordering since this is an approximate query.
Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Returns true if the buffer is full.
Note: This is a snapshot and may change immediately after returning. Uses Relaxed ordering since this is an approximate query.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the current number of items in the buffer.
Note: This is a snapshot and may change immediately after returning. Uses Relaxed ordering since this is an approximate query.
Sourcepub fn free_slots(&self) -> usize
pub fn free_slots(&self) -> usize
Returns the number of free slots in the buffer.
Note: This is a snapshot and may change immediately after returning.
Sourcepub fn pop(&self) -> Option<T>
pub fn pop(&self) -> Option<T>
Pops an item from the buffer.
Returns Some(item) if successful, or None if the buffer is empty.
§Safety
This must only be called by the single consumer thread.
Sourcepub fn peek(&self) -> Option<&T>
pub fn peek(&self) -> Option<&T>
Peeks at the next item without removing it.
Returns None if the buffer is empty.
§Safety
This must only be called by the single consumer thread.
Sourcepub fn claim_slot(&self) -> Option<usize>
pub fn claim_slot(&self) -> Option<usize>
Claims a slot for writing (MPSC mode).
Returns the slot index if successful, or None if the buffer is full.
The caller must then write to the slot and call publish_slot.
This uses atomic increment on claim_counter to ensure each producer
gets a unique slot.
Sourcepub unsafe fn write_slot(&self, slot: usize, item: T)
pub unsafe fn write_slot(&self, slot: usize, item: T)
Writes to a claimed slot (MPSC mode).
§Safety
The slot must have been obtained from claim_slot and not yet written to.
After calling this, you must call try_publish to make the item visible.
Sourcepub fn try_advance_tail(&self, target_tail: usize) -> bool
pub fn try_advance_tail(&self, target_tail: usize) -> bool
Attempts to publish written slots up to the given claim (MPSC mode).
This advances the tail if all slots up to claim have been written.
Returns true if the tail was advanced.
In MPSC mode, producers write out-of-order but the consumer sees items in order. This function is typically called by each producer after writing, but only succeeds when all prior slots are also written.
For simplicity, this implementation uses a “publish barrier” approach: producers write to their slots, then the tail is advanced by whichever producer happens to have the lowest claimed slot.
Sourcepub fn push_batch(&self, items: impl IntoIterator<Item = T>) -> usize
pub fn push_batch(&self, items: impl IntoIterator<Item = T>) -> usize
Pushes multiple items to the buffer.
Returns the number of items successfully pushed.
§Safety
In SPSC mode, this must only be called by the single producer thread.
Sourcepub fn pop_batch(&self, max_count: usize) -> Vec<T>
pub fn pop_batch(&self, max_count: usize) -> Vec<T>
Pops multiple items from the buffer.
Returns a vector of up to max_count items.
§Safety
This must only be called by the single consumer thread.
§Performance Warning
This method allocates a Vec on every call. Do not use on hot paths
where allocation overhead matters. For zero-allocation consumption, use
pop_each or pop_batch_into.
Sourcepub fn pop_each<F>(&self, max_count: usize, f: F) -> usize
pub fn pop_each<F>(&self, max_count: usize, f: F) -> usize
Pops items and calls a callback for each (zero-allocation).
Processing stops when:
max_countitems have been processed- The buffer becomes empty
- The callback returns
false
Returns the number of items processed.
§Safety
This must only be called by the single consumer thread.
Sourcepub fn pop_batch_into(&self, buffer: &mut [MaybeUninit<T>]) -> usize
pub fn pop_batch_into(&self, buffer: &mut [MaybeUninit<T>]) -> usize
Pops items into a caller-provided buffer (zero-allocation).
Returns the number of items popped.
§Safety
This must only be called by the single consumer thread.
After this method returns n, the first n elements of buffer
are initialized.
Trait Implementations§
Source§impl<T: Debug> Debug for RingBuffer<T>
impl<T: Debug> Debug for RingBuffer<T>
Source§impl<T> Drop for RingBuffer<T>
impl<T> Drop for RingBuffer<T>
impl<T: Send> Send for RingBuffer<T>
impl<T: Send> Sync for RingBuffer<T>
Auto Trait Implementations§
impl<T> !Freeze for RingBuffer<T>
impl<T> !RefUnwindSafe for RingBuffer<T>
impl<T> Unpin for RingBuffer<T>
impl<T> UnwindSafe for RingBuffer<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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
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> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.