Skip to main content

Producer

Struct Producer 

Source
pub struct Producer<T> { /* private fields */ }
Expand description

Producer handle. Move it to the producing thread; the type system enforces the SPSC invariant.

Implementations§

Source§

impl<T> Producer<T>

Source

pub fn try_push(&mut self, value: T) -> Result<(), T>

Push a value. Returns the input back as Err(value) if the buffer is full. Wait-free: at most one atomic load + one atomic store.

Examples found in repository?
examples/demo.rs (line 9)
4fn main() {
5    let (mut tx, mut rx) = SpscRingBuffer::with_capacity::<u32>(16);
6
7    let producer = thread::spawn(move || {
8        for i in 0..10u32 {
9            while tx.try_push(i).is_err() {}
10        }
11    });
12
13    let consumer = thread::spawn(move || {
14        let mut seen = Vec::new();
15        while seen.len() < 10 {
16            if let Some(v) = rx.try_pop() {
17                seen.push(v);
18            }
19        }
20        seen
21    });
22
23    producer.join().unwrap();
24    let seen = consumer.join().unwrap();
25    println!("consumed: {seen:?}");
26}
Source

pub fn capacity(&self) -> usize

Total slot count (power of two; not the requested capacity).

Trait Implementations§

Source§

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

Auto Trait Implementations§

§

impl<T> Freeze for Producer<T>

§

impl<T> !RefUnwindSafe for Producer<T>

§

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

§

impl<T> Unpin for Producer<T>

§

impl<T> UnsafeUnpin for Producer<T>

§

impl<T> !UnwindSafe 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.