Struct smol::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§

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));

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));

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));

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));

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());

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());

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());

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);

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);

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);

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);

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more
Returns true if the stream should no longer be polled.
Values yielded by the stream.
Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning None if the stream is exhausted. Read more
Returns the bounds on the remaining length of the stream. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

A convenience for calling Stream::poll_next() on !Unpin types.
Retrieves the next item in the stream. Read more
Retrieves the next item in the stream. Read more
Counts the number of items in the stream. Read more
Maps items of the stream to new values using a closure. Read more
Maps items to streams and then concatenates them. Read more
Concatenates inner streams. Read more
Maps items of the stream to new values using an async closure. Read more
Keeps items of the stream for which predicate returns true. Read more
Filters and maps items of the stream using a closure. Read more
Takes only the first n items of the stream. Read more
Takes items while predicate returns true. Read more
Skips the first n items of the stream. Read more
Skips items while predicate returns true. Read more
Yields every stepth item. Read more
Appends another stream to the end of this one. Read more
Clones all items. Read more
Copies all items. Read more
Collects all items in the stream into a collection. Read more
Collects all items in the fallible stream into a collection. Read more
Partitions items into those for which predicate is true and those for which it is false, and then collects them into two collections. Read more
Accumulates a computation over the stream. Read more
Accumulates a fallible computation over the stream. Read more
Maps items of the stream to new values using a state value and a closure. Read more
Fuses the stream so that it stops yielding items after the first None. Read more
Repeats the stream from beginning to end, forever. Read more
Enumerates items, mapping them to (index, item). Read more
Calls a closure on each item and passes it on. Read more
Gets the nth item of the stream. Read more
Returns the last item in the stream. Read more
Finds the first item of the stream for which predicate returns true. Read more
Applies a closure to items in the stream and returns the first Some result. Read more
Finds the index of the first item of the stream for which predicate returns true. Read more
Tests if predicate returns true for all items in the stream. Read more
Tests if predicate returns true for any item in the stream. Read more
Calls a closure on each item of the stream. Read more
Calls a fallible closure on each item of the stream, stopping on first error. Read more
Zips up two streams into a single stream of pairs. Read more
Collects a stream of pairs into a pair of collections. Read more
Merges with other stream, preferring items from self whenever both streams are ready. Read more
Merges with other stream, with no preference for either stream when both are ready. Read more
Boxes the stream and changes its type to dyn Stream + Send + 'a. Read more
Boxes the stream and changes its type to dyn Stream + 'a. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type of successful values yielded by this future
The type of failures yielded by this future
Poll this TryStream as if it were a Stream. Read more