Struct async_channel::Receiver
source · 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.