Struct tokio::sync::broadcast::Receiver [−][src]
pub struct Receiver<T> { /* fields omitted */ }
Expand description
Receiving-half of the broadcast
channel.
Must not be used concurrently. Messages may be retrieved using
recv
.
Examples
use tokio::sync::broadcast; #[tokio::main] async fn main() { let (tx, mut rx1) = broadcast::channel(16); let mut rx2 = tx.subscribe(); tokio::spawn(async move { assert_eq!(rx1.recv().await.unwrap(), 10); assert_eq!(rx1.recv().await.unwrap(), 20); }); tokio::spawn(async move { assert_eq!(rx2.recv().await.unwrap(), 10); assert_eq!(rx2.recv().await.unwrap(), 20); }); tx.send(10).unwrap(); tx.send(20).unwrap(); }
Implementations
impl<T> Receiver<T> where
T: Clone,
[src]
impl<T> Receiver<T> where
T: Clone,
[src]pub fn try_recv(&mut self) -> Result<T, TryRecvError>
[src]
pub fn try_recv(&mut self) -> Result<T, TryRecvError>
[src]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 one
(one for closed, one for an empty buffer, one for a lagging receiver).
Err(TryRecvError::Closed)
is returned when all Sender
halves have
dropped, indicating that no further values can be sent on the channel.
If the Receiver
handle falls behind, once the channel is full, newly
sent values will overwrite old values. At this point, a call to recv
will return with Err(TryRecvError::Lagged)
and the Receiver
’s
internal cursor is updated to point to the oldest value still held by
the channel. A subsequent call to [try_recv
] will return this value
unless it has been since overwritten. If there are no values to
receive, Err(TryRecvError::Empty)
is returned.
Examples
use tokio::sync::broadcast; #[tokio::main] async fn main() { let (tx, mut rx) = broadcast::channel(16); assert!(rx.try_recv().is_err()); tx.send(10).unwrap(); let value = rx.try_recv().unwrap(); assert_eq!(10, value); }
pub async fn recv(&mut self) -> Result<T, RecvError>
[src]
pub async fn recv(&mut self) -> Result<T, RecvError>
[src]Receives the next value for this receiver.
Each Receiver
handle will receive a clone of all values sent
after it has subscribed.
Err(RecvError::Closed)
is returned when all Sender
halves have
dropped, indicating that no further values can be sent on the channel.
If the Receiver
handle falls behind, once the channel is full, newly
sent values will overwrite old values. At this point, a call to recv
will return with Err(RecvError::Lagged)
and the Receiver
’s
internal cursor is updated to point to the oldest value still held by
the channel. A subsequent call to recv
will return this value
unless it has been since overwritten.
Examples
use tokio::sync::broadcast; #[tokio::main] async fn main() { let (tx, mut rx1) = broadcast::channel(16); let mut rx2 = tx.subscribe(); tokio::spawn(async move { assert_eq!(rx1.recv().await.unwrap(), 10); assert_eq!(rx1.recv().await.unwrap(), 20); }); tokio::spawn(async move { assert_eq!(rx2.recv().await.unwrap(), 10); assert_eq!(rx2.recv().await.unwrap(), 20); }); tx.send(10).unwrap(); tx.send(20).unwrap(); }
Handling lag
use tokio::sync::broadcast; #[tokio::main] async fn main() { let (tx, mut rx) = broadcast::channel(2); tx.send(10).unwrap(); tx.send(20).unwrap(); tx.send(30).unwrap(); // The receiver lagged behind assert!(rx.recv().await.is_err()); // At this point, we can abort or continue with lost messages assert_eq!(20, rx.recv().await.unwrap()); assert_eq!(30, rx.recv().await.unwrap()); }
Trait Implementations
impl<T> Stream for Receiver<T> where
T: Clone,
[src]
impl<T> Stream for Receiver<T> where
T: Clone,
[src]impl<T: Send> Send for Receiver<T>
[src]
impl<T: Send> Sync for Receiver<T>
[src]
Auto Trait Implementations
impl<T> !RefUnwindSafe for Receiver<T>
impl<T> Unpin for Receiver<T>
impl<T> !UnwindSafe for Receiver<T>
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<St> StreamExt for St where
St: Stream + ?Sized,
[src]
impl<St> StreamExt for St where
St: Stream + ?Sized,
[src]fn next(&mut self) -> Next<'_, Self> where
Self: Unpin,
[src]
fn next(&mut self) -> Next<'_, Self> where
Self: Unpin,
[src]Consumes and returns the next value in the stream or None
if the
stream is finished. Read more
fn try_next<T, E>(&mut self) -> TryNext<'_, Self> where
Self: Stream<Item = Result<T, E>> + Unpin,
[src]
fn try_next<T, E>(&mut self) -> TryNext<'_, Self> where
Self: Stream<Item = Result<T, E>> + Unpin,
[src]Consumes and returns the next item in the stream. If an error is encountered before the next item, the error is returned instead. Read more
fn map<T, F>(self, f: F) -> Map<Self, F> where
F: FnMut(Self::Item) -> T,
Self: Sized,
[src]
fn map<T, F>(self, f: F) -> Map<Self, F> where
F: FnMut(Self::Item) -> T,
Self: Sized,
[src]Maps this stream’s items to a different type, returning a new stream of the resulting type. Read more
fn merge<U>(self, other: U) -> Merge<Self, U> where
U: Stream<Item = Self::Item>,
Self: Sized,
[src]
fn merge<U>(self, other: U) -> Merge<Self, U> where
U: Stream<Item = Self::Item>,
Self: Sized,
[src]Combine two streams into one by interleaving the output of both as it is produced. Read more
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FnMut(&Self::Item) -> bool,
Self: Sized,
[src]
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FnMut(&Self::Item) -> bool,
Self: Sized,
[src]Filters the values produced by this stream according to the provided predicate. Read more
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F> where
F: FnMut(Self::Item) -> Option<T>,
Self: Sized,
[src]
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F> where
F: FnMut(Self::Item) -> Option<T>,
Self: Sized,
[src]Filters the values produced by this stream while simultaneously mapping them to a different type according to the provided closure. Read more
fn fuse(self) -> Fuse<Self> where
Self: Sized,
[src]
fn fuse(self) -> Fuse<Self> where
Self: Sized,
[src]Creates a stream which ends after the first None
. Read more
fn take(self, n: usize) -> Take<Self> where
Self: Sized,
[src]
fn take(self, n: usize) -> Take<Self> where
Self: Sized,
[src]Creates a new stream of at most n
items of the underlying stream. Read more
fn take_while<F>(self, f: F) -> TakeWhile<Self, F> where
F: FnMut(&Self::Item) -> bool,
Self: Sized,
[src]
fn take_while<F>(self, f: F) -> TakeWhile<Self, F> where
F: FnMut(&Self::Item) -> bool,
Self: Sized,
[src]Take elements from this stream while the provided predicate
resolves to true
. Read more
fn all<F>(&mut self, f: F) -> AllFuture<'_, Self, F> where
Self: Unpin,
F: FnMut(Self::Item) -> bool,
[src]
fn all<F>(&mut self, f: F) -> AllFuture<'_, Self, F> where
Self: Unpin,
F: FnMut(Self::Item) -> bool,
[src]Tests if every element of the stream matches a predicate. Read more
fn any<F>(&mut self, f: F) -> AnyFuture<'_, Self, F> where
Self: Unpin,
F: FnMut(Self::Item) -> bool,
[src]
fn any<F>(&mut self, f: F) -> AnyFuture<'_, Self, F> where
Self: Unpin,
F: FnMut(Self::Item) -> bool,
[src]Tests if any element of the stream matches a predicate. Read more
fn chain<U>(self, other: U) -> Chain<Self, U> where
U: Stream<Item = Self::Item>,
Self: Sized,
[src]
fn chain<U>(self, other: U) -> Chain<Self, U> where
U: Stream<Item = Self::Item>,
Self: Sized,
[src]Combine two streams into one by first returning all values from the first stream then all values from the second stream. Read more
fn fold<B, F>(self, init: B, f: F) -> FoldFuture<Self, B, F> where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
[src]
fn fold<B, F>(self, init: B, f: F) -> FoldFuture<Self, B, F> where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
[src]A combinator that applies a function to every element in a stream producing a single, final value. Read more