Struct Receiver
struct Receiver<T> { ... }
Receiving-half of the broadcast channel.
Must not be used concurrently. Messages may be retrieved using
[recv][Receiver::recv].
To turn this receiver into a Stream, you can use the BroadcastStream
wrapper.
Examples
use broadcast;
async
Implementations
impl<T> Receiver<T>
fn len(self: &Self) -> usizeReturns the number of messages that were sent into the channel and that this
Receiverhas yet to receive.If the returned value from
lenis larger than the next largest power of 2 of the capacity of the channel any call torecvwill return anErr(RecvError::Lagged)and any call totry_recvwill return anErr(TryRecvError::Lagged), e.g. if the capacity of the channel is 10,recvwill start to returnErr(RecvError::Lagged)oncelenreturns values larger than 16.Examples
use broadcast; asyncfn is_empty(self: &Self) -> boolReturns true if there aren't any messages in the channel that the
Receiverhas yet to receive.Examples
use broadcast; asyncfn same_channel(self: &Self, other: &Self) -> boolReturns
trueif receivers belong to the same channel.Examples
use broadcast; asyncfn sender_strong_count(self: &Self) -> usizeReturns the number of
Senderhandles.fn sender_weak_count(self: &Self) -> usizeReturns the number of
WeakSenderhandles.fn is_closed(self: &Self) -> boolChecks if a channel is closed.
This method returns
trueif the channel has been closed. The channel is closed when allSenderhave been dropped.Examples
use broadcast; async
impl<T: Clone> Receiver<T>
fn resubscribe(self: &Self) -> SelfRe-subscribes to the channel starting from the current tail element.
This
Receiverhandle will receive a clone of all values sent after it has resubscribed. This will not include elements that are in the queue of the current receiver. Consider the following example.Examples
use broadcast; asyncasync fn recv(self: &mut Self) -> Result<T, RecvError>Receives the next value for this receiver.
Each
Receiverhandle will receive a clone of all values sent after it has subscribed.Err(RecvError::Closed)is returned when allSenderhalves have dropped, indicating that no further values can be sent on the channel.If the
Receiverhandle falls behind, once the channel is full, newly sent values will overwrite old values. At this point, a call torecvwill return withErr(RecvError::Lagged)and theReceiver's internal cursor is updated to point to the oldest value still held by the channel. A subsequent call torecvwill return this value unless it has been since overwritten.Cancel safety
This method is cancel safe. If
recvis used as the event in atokio::select!statement and some other branch completes first, it is guaranteed that no messages were received on this channel.Examples
use broadcast; asyncHandling lag
use broadcast; asyncfn try_recv(self: &mut Self) -> Result<T, TryRecvError>Attempts to return a pending value on this receiver without awaiting.
This is useful for a flavor of "optimistic check" before deciding to await on a receiver.
Compared with
recv, this function has three failure cases instead of two (one for closed, one for an empty buffer, one for a lagging receiver).Err(TryRecvError::Closed)is returned when allSenderhalves have dropped, indicating that no further values can be sent on the channel.If the
Receiverhandle falls behind, once the channel is full, newly sent values will overwrite old values. At this point, a call torecvwill return withErr(TryRecvError::Lagged)and theReceiver's internal cursor is updated to point to the oldest value still held by the channel. A subsequent call totry_recvwill return this value unless it has been since overwritten. If there are no values to receive,Err(TryRecvError::Empty)is returned.Examples
use broadcast; asyncfn blocking_recv(self: &mut Self) -> Result<T, RecvError>Blocking receive to call outside of asynchronous contexts.
Panics
This function panics if called within an asynchronous execution context.
Examples
use thread; use broadcast; async
impl<T> Any for Receiver<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Receiver<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Receiver<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> Debug for Receiver<T>
fn fmt(self: &Self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result
impl<T> Drop for Receiver<T>
fn drop(self: &mut Self)
impl<T> Freeze for Receiver<T>
impl<T> From for Receiver<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> RefUnwindSafe for Receiver<T>
impl<T> Send for Receiver<T>
impl<T> Sync for Receiver<T>
impl<T> Unpin for Receiver<T>
impl<T> UnwindSafe for Receiver<T>
impl<T, U> Into for Receiver<T>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Receiver<T>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Receiver<T>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>