pub struct Producer<T> { /* private fields */ }Expand description
Write half of the SPSC ring. Not Clone — only one producer exists.
Implementations§
Source§impl<T> Producer<T>
impl<T> Producer<T>
Sourcepub fn is_disconnected(&self) -> bool
pub fn is_disconnected(&self) -> bool
Returns true if the consumer has been dropped.
Sourcepub fn try_push(&self, value: T) -> Result<(), TrySendError<T>>
pub fn try_push(&self, value: T) -> Result<(), TrySendError<T>>
Push a value. Returns Err if the buffer is full or the consumer has been dropped.
§Errors
TrySendError::Full— buffer is full; value is returned unchanged.TrySendError::Disconnected— consumer has been dropped; value is returned unchanged.
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 returned value can differ
from the true count in either direction: a concurrent pop can lower it,
and a stale Relaxed read of head can raise it. 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() > threshold).
§Must NOT be used for
- Deciding whether
try_pushwill succeed — use theErrreturn value oftry_pushinstead. - 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 _ = tx.len(); // safe: backpressure hint onlySource§impl<T: Copy> Producer<T>
impl<T: Copy> Producer<T>
Sourcepub fn push_slice(&self, src: &[T]) -> usize
pub fn push_slice(&self, src: &[T]) -> usize
Push as many items from src as fit. Returns count pushed.
Stops early if the buffer is full or the consumer has been dropped.
If count < src.len(), call Producer::is_disconnected to distinguish
the two cases — a full buffer is retriable, a disconnect is permanent.