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>
impl<T> Producer<T>
Sourcepub fn try_push(&mut self, value: T) -> Result<(), T>
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}Trait Implementations§
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> 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