pub struct Consumer<T> { /* private fields */ }Expand description
Read half of the SPSC ring. Not Clone — only one consumer exists.
Implementations§
Source§impl<T> Consumer<T>
impl<T> Consumer<T>
Sourcepub fn is_disconnected(&self) -> bool
pub fn is_disconnected(&self) -> bool
Returns true if the producer has been dropped.
Sourcepub fn try_pop(&self) -> Result<T, TryRecvError>
pub fn try_pop(&self) -> Result<T, TryRecvError>
Pop a value.
§Errors
TryRecvError::Empty— buffer is empty; try again later.TryRecvError::Disconnected— producer has been dropped and buffer is empty.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Approximate number of items currently in the buffer.
§Why approximate
Reads both tail (owned by the producer) and head (owned by the
consumer) with Ordering::Relaxed. The producer may have advanced
tail between the two loads, so the returned value can be lower than
the true count. The result is a best-effort snapshot, not a
linearizable read.
§Safe uses
- Capacity planning and monitoring dashboards.
- Backpressure hints (e.g., slow down if
len() < thresholdbefore sleeping).
§Must NOT be used for
- Deciding whether
try_popwill returnOk— use theErrreturn value oftry_popinstead. - Any correctness decision that requires an exact count.
§Example
use spsc_ring::ring;
let (tx, rx) = ring::<u32>(16).unwrap();
tx.try_push(1).unwrap();
tx.try_push(2).unwrap();
// len() is a hint — do not assert == 2 across threads.
let _ = rx.len(); // safe: backpressure hint onlySource§impl<T: Copy> Consumer<T>
impl<T: Copy> Consumer<T>
Sourcepub fn pop_into_slice(&self, dst: &mut [T]) -> usize
pub fn pop_into_slice(&self, dst: &mut [T]) -> usize
Pop as many items into dst as are available. Returns count popped.
Stops early if the buffer is empty or the producer has been dropped.
If count < dst.len(), call Consumer::is_disconnected to distinguish
the two cases — an empty buffer may refill, a disconnect will not.
Trait Implementations§
Auto Trait Implementations§
impl<T> !RefUnwindSafe for Consumer<T>
impl<T> !Sync for Consumer<T>
impl<T> !UnwindSafe for Consumer<T>
impl<T> Freeze for Consumer<T>
impl<T> Send for Consumer<T>where
T: Send,
impl<T> Unpin for Consumer<T>
impl<T> UnsafeUnpin for Consumer<T>
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
Mutably borrows from an owned value. Read more