pub struct Receiver<T> { /* private fields */ }
Available on crate feature
sync
only.Expand description
Receive values from the associated Sender
.
Instances are created by the channel
function.
Implementations§
Source§impl<T> Receiver<T>
impl<T> Receiver<T>
Sourcepub async fn recv(&mut self) -> Option<T>
pub async fn recv(&mut self) -> Option<T>
Receive the next value for this receiver.
None
is returned when all Sender
halves have dropped, indicating
that no further values can be sent on the channel.
§Examples
use tokio::sync::mpsc;
#[tokio::main]
async fn main() {
let (mut tx, mut rx) = mpsc::channel(100);
tokio::spawn(async move {
tx.send("hello").await.unwrap();
});
assert_eq!(Some("hello"), rx.recv().await);
assert_eq!(None, rx.recv().await);
}
Values are buffered:
use tokio::sync::mpsc;
#[tokio::main]
async fn main() {
let (mut tx, mut rx) = mpsc::channel(100);
tx.send("hello").await.unwrap();
tx.send("world").await.unwrap();
assert_eq!(Some("hello"), rx.recv().await);
assert_eq!(Some("world"), rx.recv().await);
}
Sourcepub fn try_recv(&mut self) -> Result<T, TryRecvError>
pub fn try_recv(&mut self) -> Result<T, TryRecvError>
Attempts to return a pending value on this receiver without blocking.
This method will never block the caller in order to wait for data to become available. Instead, this will always return immediately with a possible option of pending data on the channel.
This is useful for a flavor of “optimistic check” before deciding to block on a receiver.
Compared with recv, this function has two failure cases instead of one (one for disconnection, one for an empty buffer).
Trait Implementations§
Source§impl<T> Stream for Receiver<T>
Available on crate feature stream
only.
impl<T> Stream for Receiver<T>
Available on crate feature
stream
only.impl<T> Unpin for Receiver<T>
Auto Trait Implementations§
impl<T> Freeze for Receiver<T>
impl<T> !RefUnwindSafe for Receiver<T>
impl<T> Send for Receiver<T>where
T: Send,
impl<T> Sync for Receiver<T>where
T: Send,
impl<T> !UnwindSafe for Receiver<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)Source§impl<St> StreamExt for St
impl<St> StreamExt for St
Source§fn next(&mut self) -> Next<'_, Self>where
Self: Unpin,
fn next(&mut self) -> Next<'_, Self>where
Self: Unpin,
Available on crate feature
stream
only.Consumes and returns the next value in the stream or
None
if the
stream is finished. Read moreSource§fn try_next<T, E>(&mut self) -> TryNext<'_, Self>
fn try_next<T, E>(&mut self) -> TryNext<'_, Self>
Available on crate feature
stream
only.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
Source§fn map<T, F>(self, f: F) -> Map<Self, F>
fn map<T, F>(self, f: F) -> Map<Self, F>
Available on crate feature
stream
only.Maps this stream’s items to a different type, returning a new stream of
the resulting type. Read more
Source§fn merge<U>(self, other: U) -> Merge<Self, U>
fn merge<U>(self, other: U) -> Merge<Self, U>
Available on crate feature
stream
only.Combine two streams into one by interleaving the output of both as it
is produced. Read more
Source§fn filter<F>(self, f: F) -> Filter<Self, F>
fn filter<F>(self, f: F) -> Filter<Self, F>
Available on crate feature
stream
only.Filters the values produced by this stream according to the provided
predicate. Read more
Source§fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
Available on crate feature
stream
only.Filters the values produced by this stream while simultaneously mapping
them to a different type according to the provided closure. Read more
Source§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
Available on crate feature
stream
only.Creates a stream which ends after the first
None
. Read moreSource§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
Available on crate feature
stream
only.Creates a new stream of at most
n
items of the underlying stream. Read moreSource§fn take_while<F>(self, f: F) -> TakeWhile<Self, F>
fn take_while<F>(self, f: F) -> TakeWhile<Self, F>
Available on crate feature
stream
only.Take elements from this stream while the provided predicate
resolves to
true
. Read moreSource§fn all<F>(&mut self, f: F) -> AllFuture<'_, Self, F>
fn all<F>(&mut self, f: F) -> AllFuture<'_, Self, F>
Available on crate feature
stream
only.Tests if every element of the stream matches a predicate. Read more
Source§fn any<F>(&mut self, f: F) -> AnyFuture<'_, Self, F>
fn any<F>(&mut self, f: F) -> AnyFuture<'_, Self, F>
Available on crate feature
stream
only.Tests if any element of the stream matches a predicate. Read more