pub struct Receiver<T> { /* private fields */ }
Expand description
The receiving side of a channel.
Receivers can be cloned and shared among threads. When all receivers associated with a channel are dropped, the channel becomes closed.
The channel can also be closed manually by calling Receiver::close()
.
Receivers implement the Stream
trait.
ImplementationsΒ§
SourceΒ§impl<T> Receiver<T>
impl<T> Receiver<T>
Sourcepub fn try_recv(&self) -> Result<T, TryRecvError>
pub fn try_recv(&self) -> Result<T, TryRecvError>
Attempts to receive a message from the channel.
If the channel is empty, or empty and closed, this method returns an error.
Β§Examples
use async_channel::{unbounded, TryRecvError};
let (s, r) = unbounded();
assert_eq!(s.send(1).await, Ok(()));
assert_eq!(r.try_recv(), Ok(1));
assert_eq!(r.try_recv(), Err(TryRecvError::Empty));
drop(s);
assert_eq!(r.try_recv(), Err(TryRecvError::Closed));
Sourcepub fn recv(&self) -> Recv<'_, T> β
pub fn recv(&self) -> Recv<'_, T> β
Receives a message from the channel.
If the channel is empty, this method waits until there is a message.
If the channel is closed, this method receives a message or returns an error if there are no more messages.
Β§Examples
use async_channel::{unbounded, RecvError};
let (s, r) = unbounded();
assert_eq!(s.send(1).await, Ok(()));
drop(s);
assert_eq!(r.recv().await, Ok(1));
assert_eq!(r.recv().await, Err(RecvError));
Sourcepub fn recv_blocking(&self) -> Result<T, RecvError>
pub fn recv_blocking(&self) -> Result<T, RecvError>
Receives a message from the channel using the blocking strategy.
If the channel is empty, this method waits until there is a message. If the channel is closed, this method receives a message or returns an error if there are no more messages.
Β§Blocking
Rather than using asynchronous waiting, like the recv
method,
this method will block the current thread until the message is sent.
This method should not be used in an asynchronous context. It is intended to be used such that a channel can be used in both asynchronous and synchronous contexts. Calling this method in an asynchronous context may result in deadlocks.
Β§Examples
use async_channel::{unbounded, RecvError};
let (s, r) = unbounded();
assert_eq!(s.send_blocking(1), Ok(()));
drop(s);
assert_eq!(r.recv_blocking(), Ok(1));
assert_eq!(r.recv_blocking(), Err(RecvError));
Sourcepub fn close(&self) -> bool
pub fn close(&self) -> bool
Closes the channel.
Returns true
if this call has closed the channel and it was not closed already.
The remaining messages can still be received.
Β§Examples
use async_channel::{unbounded, RecvError};
let (s, r) = unbounded();
assert_eq!(s.send(1).await, Ok(()));
assert!(r.close());
assert_eq!(r.recv().await, Ok(1));
assert_eq!(r.recv().await, Err(RecvError));
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Returns true
if the channel is closed.
Β§Examples
use async_channel::{unbounded, RecvError};
let (s, r) = unbounded::<()>();
assert!(!r.is_closed());
drop(s);
assert!(r.is_closed());
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the channel is empty.
Β§Examples
use async_channel::unbounded;
let (s, r) = unbounded();
assert!(s.is_empty());
s.send(1).await;
assert!(!s.is_empty());
Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Returns true
if the channel is full.
Unbounded channels are never full.
Β§Examples
use async_channel::bounded;
let (s, r) = bounded(1);
assert!(!r.is_full());
s.send(1).await;
assert!(r.is_full());
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of messages in the channel.
Β§Examples
use async_channel::unbounded;
let (s, r) = unbounded();
assert_eq!(r.len(), 0);
s.send(1).await;
s.send(2).await;
assert_eq!(r.len(), 2);
Sourcepub fn capacity(&self) -> Option<usize>
pub fn capacity(&self) -> Option<usize>
Returns the channel capacity if itβs bounded.
Β§Examples
use async_channel::{bounded, unbounded};
let (s, r) = bounded::<i32>(5);
assert_eq!(r.capacity(), Some(5));
let (s, r) = unbounded::<i32>();
assert_eq!(r.capacity(), None);
Sourcepub fn receiver_count(&self) -> usize
pub fn receiver_count(&self) -> usize
Returns the number of receivers for the channel.
Β§Examples
use async_channel::unbounded;
let (s, r) = unbounded::<()>();
assert_eq!(r.receiver_count(), 1);
let r2 = r.clone();
assert_eq!(r.receiver_count(), 2);
Sourcepub fn sender_count(&self) -> usize
pub fn sender_count(&self) -> usize
Returns the number of senders for the channel.
Β§Examples
use async_channel::unbounded;
let (s, r) = unbounded::<()>();
assert_eq!(r.sender_count(), 1);
let s2 = s.clone();
assert_eq!(r.sender_count(), 2);
Sourcepub fn downgrade(&self) -> WeakReceiver<T>
pub fn downgrade(&self) -> WeakReceiver<T>
Downgrade the receiver to a weak reference.
Trait ImplementationsΒ§
SourceΒ§impl<T> FusedStream for Receiver<T>
impl<T> FusedStream for Receiver<T>
SourceΒ§fn is_terminated(&self) -> bool
fn is_terminated(&self) -> bool
true
if the stream should no longer be polled.SourceΒ§impl<T> Stream for Receiver<T>
impl<T> Stream for Receiver<T>
SourceΒ§fn poll_next(
self: Pin<&mut Receiver<T>>,
cx: &mut Context<'_>,
) -> Poll<Option<<Receiver<T> as Stream>::Item>>
fn poll_next( self: Pin<&mut Receiver<T>>, cx: &mut Context<'_>, ) -> Poll<Option<<Receiver<T> as Stream>::Item>>
None
if the stream is exhausted. Read moreAuto 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> Unpin for Receiver<T>
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
SourceΒ§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
SourceΒ§impl<I> IntoStream for Iwhere
I: Stream,
impl<I> IntoStream for Iwhere
I: Stream,
SourceΒ§impl<S> StreamExt for S
impl<S> StreamExt for S
SourceΒ§fn next(&mut self) -> NextFuture<'_, Self> βwhere
Self: Unpin,
fn next(&mut self) -> NextFuture<'_, Self> βwhere
Self: Unpin,
SourceΒ§fn try_next<T, E>(&mut self) -> TryNextFuture<'_, Self> β
fn try_next<T, E>(&mut self) -> TryNextFuture<'_, Self> β
SourceΒ§fn count(self) -> CountFuture<Self> βwhere
Self: Sized,
fn count(self) -> CountFuture<Self> βwhere
Self: Sized,
SourceΒ§fn map<T, F>(self, f: F) -> Map<Self, F>
fn map<T, F>(self, f: F) -> Map<Self, F>
SourceΒ§fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
SourceΒ§fn then<F, Fut>(self, f: F) -> Then<Self, F, Fut>
fn then<F, Fut>(self, f: F) -> Then<Self, F, Fut>
SourceΒ§fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
SourceΒ§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
n
items of the stream. Read moreSourceΒ§fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
SourceΒ§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n
items of the stream. Read moreSourceΒ§fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
SourceΒ§fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
step
th item. Read moreSourceΒ§fn chain<U>(self, other: U) -> Chain<Self, U>
fn chain<U>(self, other: U) -> Chain<Self, U>
SourceΒ§fn collect<C>(self) -> CollectFuture<Self, C> β
fn collect<C>(self) -> CollectFuture<Self, C> β
SourceΒ§fn try_collect<T, E, C>(self) -> TryCollectFuture<Self, C> β
fn try_collect<T, E, C>(self) -> TryCollectFuture<Self, C> β
SourceΒ§fn partition<B, P>(self, predicate: P) -> PartitionFuture<Self, P, B> β
fn partition<B, P>(self, predicate: P) -> PartitionFuture<Self, P, B> β
predicate
is true
and those for which it is
false
, and then collects them into two collections. Read moreSourceΒ§fn fold<T, F>(self, init: T, f: F) -> FoldFuture<Self, F, T> β
fn fold<T, F>(self, init: T, f: F) -> FoldFuture<Self, F, T> β
SourceΒ§fn try_fold<T, E, F, B>(
&mut self,
init: B,
f: F,
) -> TryFoldFuture<'_, Self, F, B> β
fn try_fold<T, E, F, B>( &mut self, init: B, f: F, ) -> TryFoldFuture<'_, Self, F, B> β
SourceΒ§fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>
fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>
SourceΒ§fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
(index, item)
. Read moreSourceΒ§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
SourceΒ§fn nth(&mut self, n: usize) -> NthFuture<'_, Self> βwhere
Self: Unpin,
fn nth(&mut self, n: usize) -> NthFuture<'_, Self> βwhere
Self: Unpin,
n
th item of the stream. Read moreSourceΒ§fn last(self) -> LastFuture<Self> βwhere
Self: Sized,
fn last(self) -> LastFuture<Self> βwhere
Self: Sized,
SourceΒ§fn find<P>(&mut self, predicate: P) -> FindFuture<'_, Self, P> β
fn find<P>(&mut self, predicate: P) -> FindFuture<'_, Self, P> β
SourceΒ§fn find_map<F, B>(&mut self, f: F) -> FindMapFuture<'_, Self, F> β
fn find_map<F, B>(&mut self, f: F) -> FindMapFuture<'_, Self, F> β
SourceΒ§fn position<P>(&mut self, predicate: P) -> PositionFuture<'_, Self, P> β
fn position<P>(&mut self, predicate: P) -> PositionFuture<'_, Self, P> β
SourceΒ§fn for_each<F>(self, f: F) -> ForEachFuture<Self, F> β
fn for_each<F>(self, f: F) -> ForEachFuture<Self, F> β
SourceΒ§fn try_for_each<F, E>(&mut self, f: F) -> TryForEachFuture<'_, Self, F> β
fn try_for_each<F, E>(&mut self, f: F) -> TryForEachFuture<'_, Self, F> β
SourceΒ§fn zip<U>(self, other: U) -> Zip<Self, U>
fn zip<U>(self, other: U) -> Zip<Self, U>
SourceΒ§fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB> β
fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB> β
SourceΒ§fn race<S>(self, other: S) -> Race<Self, S>
fn race<S>(self, other: S) -> Race<Self, S>
other
stream, with no preference for either stream when both are ready. Read moreSourceΒ§fn drain(&mut self) -> Drain<'_, Self>
fn drain(&mut self) -> Drain<'_, Self>
SourceΒ§impl<T> StreamExt for T
impl<T> StreamExt for T
SourceΒ§fn next(&mut self) -> NextFuture<'_, Self>where
Self: Unpin,
fn next(&mut self) -> NextFuture<'_, Self>where
Self: Unpin,
SourceΒ§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
n
elements. Read moreSourceΒ§fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
SourceΒ§fn throttle(self, d: Duration) -> Throttle<Self>where
Self: Sized,
fn throttle(self, d: Duration) -> Throttle<Self>where
Self: Sized,
unstable
only.SourceΒ§fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
step
th element. Read moreSourceΒ§fn chain<U>(self, other: U) -> Chain<Self, U>
fn chain<U>(self, other: U) -> Chain<Self, U>
SourceΒ§fn cloned<'a, T>(self) -> Cloned<Self>
fn cloned<'a, T>(self) -> Cloned<Self>
SourceΒ§fn copied<'a, T>(self) -> Copied<Self>
fn copied<'a, T>(self) -> Copied<Self>
SourceΒ§fn cycle(self) -> Cycle<Self>
fn cycle(self) -> Cycle<Self>
SourceΒ§fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
SourceΒ§fn delay(self, dur: Duration) -> Delay<Self>where
Self: Sized,
fn delay(self, dur: Duration) -> Delay<Self>where
Self: Sized,
unstable
only.SourceΒ§fn map<B, F>(self, f: F) -> Map<Self, F>
fn map<B, F>(self, f: F) -> Map<Self, F>
SourceΒ§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
SourceΒ§fn last(self) -> LastFuture<Self, Self::Item>where
Self: Sized,
fn last(self) -> LastFuture<Self, Self::Item>where
Self: Sized,
SourceΒ§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
None
. Read moreSourceΒ§fn filter<P>(self, predicate: P) -> Filter<Self, P>
fn filter<P>(self, predicate: P) -> Filter<Self, P>
SourceΒ§fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
unstable
only.SourceΒ§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
unstable
only.SourceΒ§fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F>
fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F>
SourceΒ§fn min_by_key<B, F>(self, key_by: F) -> MinByKeyFuture<Self, Self::Item, F>
fn min_by_key<B, F>(self, key_by: F) -> MinByKeyFuture<Self, Self::Item, F>
None
is returned. Read moreSourceΒ§fn max_by_key<B, F>(self, key_by: F) -> MaxByKeyFuture<Self, Self::Item, F>
fn max_by_key<B, F>(self, key_by: F) -> MaxByKeyFuture<Self, Self::Item, F>
None
is returned. Read moreSourceΒ§fn min_by<F>(self, compare: F) -> MinByFuture<Self, F, Self::Item>
fn min_by<F>(self, compare: F) -> MinByFuture<Self, F, Self::Item>
None
is returned. Read moreSourceΒ§fn max(self) -> MaxFuture<Self, Self::Item>
fn max(self) -> MaxFuture<Self, Self::Item>
None
is returned. Read moreSourceΒ§fn min(self) -> MinFuture<Self, Self::Item>
fn min(self) -> MinFuture<Self, Self::Item>
None
is returned. Read moreSourceΒ§fn max_by<F>(self, compare: F) -> MaxByFuture<Self, F, Self::Item>
fn max_by<F>(self, compare: F) -> MaxByFuture<Self, F, Self::Item>
None
is returned. Read moreSourceΒ§fn nth(&mut self, n: usize) -> NthFuture<'_, Self>
fn nth(&mut self, n: usize) -> NthFuture<'_, Self>
SourceΒ§fn all<F>(&mut self, f: F) -> AllFuture<'_, Self, F, Self::Item>
fn all<F>(&mut self, f: F) -> AllFuture<'_, Self, F, Self::Item>
SourceΒ§fn find<P>(&mut self, p: P) -> FindFuture<'_, Self, P>
fn find<P>(&mut self, p: P) -> FindFuture<'_, Self, P>
SourceΒ§fn find_map<F, B>(&mut self, f: F) -> FindMapFuture<'_, Self, F>
fn find_map<F, B>(&mut self, f: F) -> FindMapFuture<'_, Self, F>
SourceΒ§fn fold<B, F>(self, init: B, f: F) -> FoldFuture<Self, F, B>
fn fold<B, F>(self, init: B, f: F) -> FoldFuture<Self, F, B>
SourceΒ§fn partition<B, F>(self, f: F) -> PartitionFuture<Self, F, B>
fn partition<B, F>(self, f: F) -> PartitionFuture<Self, F, B>
unstable
only.SourceΒ§fn for_each<F>(self, f: F) -> ForEachFuture<Self, F>
fn for_each<F>(self, f: F) -> ForEachFuture<Self, F>
SourceΒ§fn any<F>(&mut self, f: F) -> AnyFuture<'_, Self, F, Self::Item>
fn any<F>(&mut self, f: F) -> AnyFuture<'_, Self, F, Self::Item>
SourceΒ§fn by_ref(&mut self) -> &mut Self
fn by_ref(&mut self) -> &mut Self
unstable
only.SourceΒ§fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
skip
s elements based on a predicate. Read moreSourceΒ§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n
elements. Read moreSourceΒ§fn timeout(self, dur: Duration) -> Timeout<Self>
fn timeout(self, dur: Duration) -> Timeout<Self>
unstable
only.SourceΒ§fn try_fold<B, F, T, E>(
&mut self,
init: T,
f: F,
) -> TryFoldFuture<'_, Self, F, T>
fn try_fold<B, F, T, E>( &mut self, init: T, f: F, ) -> TryFoldFuture<'_, Self, F, T>
SourceΒ§fn try_for_each<F, E>(&mut self, f: F) -> TryForEachFuture<'_, Self, F>
fn try_for_each<F, E>(&mut self, f: F) -> TryForEachFuture<'_, Self, F>
SourceΒ§fn zip<U>(self, other: U) -> Zip<Self, U>
fn zip<U>(self, other: U) -> Zip<Self, U>
SourceΒ§fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB>
fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB>
unstable
only.SourceΒ§fn collect<'a, B>(self) -> Pin<Box<dyn Future<Output = B> + Send + 'a>>
fn collect<'a, B>(self) -> Pin<Box<dyn Future<Output = B> + Send + 'a>>
unstable
only.SourceΒ§fn merge<U>(self, other: U) -> Merge<Self, U>
fn merge<U>(self, other: U) -> Merge<Self, U>
unstable
only.SourceΒ§fn partial_cmp<S>(self, other: S) -> PartialCmpFuture<Self, S>
fn partial_cmp<S>(self, other: S) -> PartialCmpFuture<Self, S>
Stream
with those
of another. Read moreSourceΒ§fn position<P>(&mut self, predicate: P) -> PositionFuture<'_, Self, P>
fn position<P>(&mut self, predicate: P) -> PositionFuture<'_, Self, P>
SourceΒ§fn cmp<S>(self, other: S) -> CmpFuture<Self, S>
fn cmp<S>(self, other: S) -> CmpFuture<Self, S>
Stream
with those
of another using βOrdβ. Read moreSourceΒ§fn count(self) -> CountFuture<Self>where
Self: Sized,
fn count(self) -> CountFuture<Self>where
Self: Sized,
unstable
only.SourceΒ§fn ne<S>(self, other: S) -> NeFuture<Self, S>
fn ne<S>(self, other: S) -> NeFuture<Self, S>
Stream
are lexicographically
not equal to those of another. Read moreSourceΒ§fn ge<S>(self, other: S) -> GeFuture<Self, S>
fn ge<S>(self, other: S) -> GeFuture<Self, S>
Stream
are lexicographically
greater than or equal to those of another. Read moreSourceΒ§fn eq<S>(self, other: S) -> EqFuture<Self, S>
fn eq<S>(self, other: S) -> EqFuture<Self, S>
Stream
are lexicographically
equal to those of another. Read moreSourceΒ§fn gt<S>(self, other: S) -> GtFuture<Self, S>
fn gt<S>(self, other: S) -> GtFuture<Self, S>
Stream
are lexicographically
greater than those of another. Read moreSourceΒ§fn le<S>(self, other: S) -> LeFuture<Self, S>
fn le<S>(self, other: S) -> LeFuture<Self, S>
Stream
are lexicographically
less or equal to those of another. Read moreSourceΒ§fn lt<S>(self, other: S) -> LtFuture<Self, S>
fn lt<S>(self, other: S) -> LtFuture<Self, S>
Stream
are lexicographically
less than those of another. Read more