Skip to main content

Producer

Struct Producer 

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

Source

pub fn is_disconnected(&self) -> bool

Returns true if the consumer has been dropped.

Source

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
Source

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_push will succeed — use the Err return value of try_push instead.
  • 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 only
Source

pub fn is_empty(&self) -> bool

Returns true if the buffer appears empty.

Source

pub fn is_full(&self) -> bool

Returns true if the buffer appears full.

Source

pub fn capacity(&self) -> usize

Buffer capacity.

Source

pub fn push( &self, value: T, strategy: &WaitStrategy, ) -> Result<(), SendError<T>>

Push a value, blocking with strategy until a slot is free.

§Errors

Returns SendError containing the value if the consumer has been dropped.

Source§

impl<T: Copy> Producer<T>

Source

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.

Trait Implementations§

Source§

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

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Producer<T>

§

impl<T> !Sync for Producer<T>

§

impl<T> !UnwindSafe for Producer<T>

§

impl<T> Freeze for Producer<T>

§

impl<T> Send for Producer<T>
where T: Send,

§

impl<T> Unpin for Producer<T>

§

impl<T> UnsafeUnpin for Producer<T>

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.