Struct Receiver
pub struct Receiver<T> { /* private fields */ }
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) -> usizeReturns the number of messages that were sent into the channel and that this
Receiverhas yet to receive.This count includes messages that have already been overwritten in the ring buffer and are no longer readable. If
lenis greater than the channel's effective capacity (the provided capacity rounded up to the next power of two), the next call torecvreturnsErr(RecvError::Lagged)and the next call totry_recvreturnsErr(TryRecvError::Lagged). For example, withchannel(10)the buffer length is 16, so lagging begins oncelenis larger than 16.After a successful receive (including after handling
Laggedand then reading retained messages),lendecreases accordingly.Examples
use broadcast; # # asyncfn is_empty(&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, other: &Self) -> boolReturns
trueif receivers belong to the same channel.Examples
use broadcast; # # asyncfn sender_strong_count(&self) -> usizeReturns the number of
Senderhandles.fn sender_weak_count(&self) -> usizeReturns the number of
WeakSenderhandles.fn is_closed(&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) -> 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(&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 overwrite old values in the ring buffer. The next call torecvthen returnsErr(RecvError::Lagged(n)), wherenis the number of overwritten messages the receiver missed. The receiver stays subscribed; its internal cursor is advanced to the oldest value still held by the channel. A subsequent call torecvreturns that value, unless further sends overwrite it before the receiver reads it. See lagging for details.Cancel safety
This method is cancel safe. If
recvis used as a branch intokio::select!and another branch completes first, it is guaranteed that no messages were received on this channel.Examples
use broadcast; # # asyncHandling lag
use broadcast; use RecvError; # # asyncfn try_recv(&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 overwrite old values in the ring buffer. The next call totry_recvthen returnsErr(TryRecvError::Lagged(n)), wherenis the number of overwritten messages the receiver missed. The receiver stays subscribed; its internal cursor is advanced to the oldest value still held by the channel. A subsequent call totry_recvreturns that value, unless further sends overwrite it before the receiver reads it. If there are no values to receive,Err(TryRecvError::Empty)is returned. See lagging for details.Examples
use broadcast; # # asyncfn blocking_recv(&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
# #
Trait Implementations
impl<T> Debug for Receiver<T>
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result
impl<T> Drop for Receiver<T>
fn drop(&mut self)
Auto Trait Implementations
impl<T> !RefUnwindSafe for Receiver<T>
impl<T> !UnwindSafe for Receiver<T>
impl<T> Freeze for Receiver<T>
where
Arc<Shared<T>>: Freeze,
impl<T> Send for Receiver<T>
where
Arc<Shared<T>>: Send,
impl<T> Sync for Receiver<T>
where
Arc<Shared<T>>: Sync,
impl<T> Unpin for Receiver<T>
where
Arc<Shared<T>>: Unpin,
impl<T> UnsafeUnpin for Receiver<T>
where
Arc<Shared<T>>: UnsafeUnpin,
Blanket Implementations
impl<T> Any for Receiver<T>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Receiver<T>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Receiver<T>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for Receiver<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for Receiver<T>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Receiver<T>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Receiver<T>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>