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§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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 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 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 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 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 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 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 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 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 more