pub struct Consumer<T> { /* private fields */ }
Expand description
Fifo Consumption Handle
This handle allows you to pull items or batches of items from the FIFO, atomically.
Under the hood, most methods call Self::try_recv_into
,
which knows how many items to pull from the FIFO based on
the constraints set by its Storage
parameter. For instance,
Self::try_recv_exact
has the effect of pulling either
zero or exactly N items, depending on how many items are
available in the FIFO.
Items are pulled in the exact same order as they were pushed.
Implementations§
Source§impl<T> Consumer<T>
impl<T> Consumer<T>
Sourcepub fn no_producers(&self) -> bool
pub fn no_producers(&self) -> bool
Returns true if all producers have been dropped
§Example
let (tx, rx) = async_fifo::new();
tx.send('z');
// one remaining producer
assert_eq!(rx.no_producers(), false);
// drop it
core::mem::drop(tx);
// all producers are gone
assert_eq!(rx.no_producers(), true);
// No producer, yes, but one item is still in there.
assert_eq!(rx.try_recv(), Some('z'));
assert_eq!(rx.try_recv(), None);
Sourcepub fn try_recv(&self) -> Option<T>
pub fn try_recv(&self) -> Option<T>
Tries to receive one item.
§Example
let (tx, rx) = async_fifo::new();
tx.send_iter(['a', 'b', 'c']);
// Receive one by one
assert_eq!(rx.try_recv(), Some('a'));
assert_eq!(rx.try_recv(), Some('b'));
assert_eq!(rx.try_recv(), Some('c'));
assert_eq!(rx.try_recv(), None);
Sourcepub fn try_recv_many(&self, vec: &mut Vec<T>) -> Option<usize>
pub fn try_recv_many(&self, vec: &mut Vec<T>) -> Option<usize>
Tries to receive as many items as possible, into a vector.
If at least one item is received, the number of received items is returned.
§Example
let (tx, rx) = async_fifo::new();
tx.send_iter(['a', 'b', 'c', 'd']);
// Pull as much as possible into a vec
let mut bucket = Vec::new();
assert_eq!(rx.try_recv_many(&mut bucket), Some(4));
assert_eq!(bucket, ['a', 'b', 'c', 'd']);
assert_eq!(rx.try_recv(), None);
Sourcepub fn try_recv_exact(&self, slice: &mut [T]) -> Option<()>
pub fn try_recv_exact(&self, slice: &mut [T]) -> Option<()>
Tries to receive exactly slice.len()
items into a slice.
§Example
let (tx, rx) = async_fifo::new();
tx.send_iter(['a', 'b', 'c']);
// Pull a specific amount into a slice
let mut buffer = ['_'; 3];
assert!(rx.try_recv_exact(&mut buffer).is_some());
assert_eq!(buffer, ['a', 'b', 'c']);
assert_eq!(rx.try_recv(), None);
Sourcepub fn try_recv_array<const N: usize>(&self) -> Option<[T; N]>
pub fn try_recv_array<const N: usize>(&self) -> Option<[T; N]>
Tries to receive exactly N
items into an array.
§Example
let (tx, rx) = async_fifo::new();
tx.send_iter(['a', 'b', 'c']);
// Pull a specific amount into an array
assert_eq!(rx.try_recv_array(), Some(['a', 'b', 'c']));
assert_eq!(rx.try_recv(), None);
Sourcepub fn try_recv_into<S: Storage<T>>(&self, storage: S) -> Result<S::Output, S>
pub fn try_recv_into<S: Storage<T>>(&self, storage: S) -> Result<S::Output, S>
Tries to receive some items into custom storage.