pub struct Receiver<T: Send + Sync + 'static> { /* private fields */ }Expand description
Receives the values of the channel, in order.
Created by channel, Sender::subscribe or
Receiver::resubscribe; each takes every value sent after it was made.
Implementations§
Source§impl<T: Send + Sync + 'static> Receiver<T>
impl<T: Send + Sync + 'static> Receiver<T>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
How many values were sent that this receiver has yet to take; past the capacity, its next receive reports a lag.
Sourcepub fn resubscribe(&self) -> Self
pub fn resubscribe(&self) -> Self
A new receiver, which takes the values sent from now on.
Source§impl<T: Clone + Send + Sync + 'static> Receiver<T>
impl<T: Clone + Send + Sync + 'static> Receiver<T>
Sourcepub fn try_recv(&mut self) -> Result<T, TryRecvError>
pub fn try_recv(&mut self) -> Result<T, TryRecvError>
Takes the next value, if one came, without waiting.
§Errors
TryRecvError::Empty when none came since the last one taken;
TryRecvError::Lagged once, when the receiver lost values, before
it goes on from the oldest one still held; TryRecvError::Closed
once every sender is gone and every value was taken.
use nuclei::sync::broadcast::{self, error::TryRecvError};
let (tx, mut rx) = broadcast::channel(2);
for i in 0..3 {
tx.send(i).unwrap();
}
assert_eq!(rx.try_recv(), Err(TryRecvError::Lagged(1)));
assert_eq!(rx.try_recv(), Ok(1));
assert_eq!(rx.try_recv(), Ok(2));
assert_eq!(rx.try_recv(), Err(TryRecvError::Empty));
drop(tx);
assert_eq!(rx.try_recv(), Err(TryRecvError::Closed));Sourcepub async fn recv(&mut self) -> Result<T, RecvError>
pub async fn recv(&mut self) -> Result<T, RecvError>
Waits for the next value and takes it.
§Errors
RecvError::Lagged once, when the receiver lost values, before it
goes on from the oldest one still held; RecvError::Closed once
every sender is gone and every value was taken.
§Cancel safety
Dropping the future takes nothing: the next receive gets the value.
§Cooperative scheduling
On a nuclei shard a receive that completes spends one operation of
its task’s budget; with the budget spent it takes nothing and yields
to the shard’s other tasks first (see nuclei::task::coop).
Sourcepub fn blocking_recv(&mut self) -> Result<T, RecvError>
pub fn blocking_recv(&mut self) -> Result<T, RecvError>
Receives the next value from a thread that may block.
Like recv, but it parks the calling thread while no
value came and the channel is open. On a nuclei shard (inside one of
its tasks) it does not park the thread: it runs a nested block_on,
so the shard keeps running its other tasks meanwhile. Inside another
runtime’s task it blocks that task’s thread.
§Errors
As for recv.
§Panics
On wasm32, where no other thread can send, when no value came and
the channel is open: the call could never end.
use nuclei::sync::broadcast::{self, RecvError};
let (tx, mut rx) = broadcast::channel::<u8>(4);
let receiver = std::thread::spawn(move || {
assert_eq!(rx.blocking_recv(), Ok(10));
assert_eq!(rx.blocking_recv(), Err(RecvError::Closed));
});
tx.send(10).unwrap();
drop(tx);
receiver.join().unwrap();