Struct Receiver
struct Receiver<T> { ... }
Receives values from the associated Sender.
Instances are created by the channel function.
This receiver can be turned into a Stream using ReceiverStream.
Implementations
impl<T> Receiver<T>
async fn recv(self: &mut Self) -> Option<T>Receives the next value for this receiver.
This method returns
Noneif the channel has been closed and there are no remaining messages in the channel's buffer. This indicates that no further values can ever be received from thisReceiver. The channel is closed when all senders have been dropped, or whencloseis called.If there are no messages in the channel's buffer, but the channel has not yet been closed, this method will sleep until a message is sent or the channel is closed. Note that if
closeis called, but there are still outstandingPermitsfrom before it was closed, the channel is not considered closed byrecvuntil the permits are released.Cancel safety
This method is cancel safe. If
recvis used as the event in atokio::select!statement and some other branch completes first, it is guaranteed that no messages were received on this channel.Examples
use mpsc; asyncValues are buffered:
use mpsc; asyncasync fn recv_many(self: &mut Self, buffer: &mut Vec<T>, limit: usize) -> usizeReceives the next values for this receiver and extends
buffer.This method extends
bufferby no more than a fixed number of values as specified bylimit. Iflimitis zero, the function immediately returns0. The return value is the number of values added tobuffer.For
limit > 0, if there are no messages in the channel's queue, but the channel has not yet been closed, this method will sleep until a message is sent or the channel is closed. Note that ifcloseis called, but there are still outstandingPermitsfrom before it was closed, the channel is not considered closed byrecv_manyuntil the permits are released.For non-zero values of
limit, this method will never return0unless the channel has been closed and there are no remaining messages in the channel's queue. This indicates that no further values can ever be received from thisReceiver. The channel is closed when all senders have been dropped, or whencloseis called.The capacity of
bufferis increased as needed.Cancel safety
This method is cancel safe. If
recv_manyis used as the event in atokio::select!statement and some other branch completes first, it is guaranteed that no messages were received on this channel.Examples
use mpsc; asyncfn try_recv(self: &mut Self) -> Result<T, TryRecvError>Tries to receive the next value for this receiver.
This method returns the
Emptyerror if the channel is currently empty, but there are still outstanding senders or permits.This method returns the
Disconnectederror if the channel is currently empty, and there are no outstanding senders or permits.Unlike the
poll_recvmethod, this method will never return anEmptyerror spuriously.Examples
use mpsc; use TryRecvError; asyncfn blocking_recv(self: &mut Self) -> Option<T>Blocking receive to call outside of asynchronous contexts.
This method returns
Noneif the channel has been closed and there are no remaining messages in the channel's buffer. This indicates that no further values can ever be received from thisReceiver. The channel is closed when all senders have been dropped, or whencloseis called.If there are no messages in the channel's buffer, but the channel has not yet been closed, this method will block until a message is sent or the channel is closed.
This method is intended for use cases where you are sending from asynchronous code to synchronous code, and will work even if the sender is not using
blocking_sendto send the message.Note that if
closeis called, but there are still outstandingPermitsfrom before it was closed, the channel is not considered closed byblocking_recvuntil the permits are released.Panics
This function panics if called within an asynchronous execution context.
Examples
use thread; use Runtime; use mpsc;fn blocking_recv_many(self: &mut Self, buffer: &mut Vec<T>, limit: usize) -> usizeVariant of
Self::recv_manyfor blocking contexts.The same conditions as in
Self::blocking_recvapply.fn close(self: &mut Self)Closes the receiving half of a channel without dropping it.
This prevents any further messages from being sent on the channel while still enabling the receiver to drain messages that are buffered. Any outstanding
Permitvalues will still be able to send messages.To guarantee that no messages are dropped, after calling
close(),recv()must be called untilNoneis returned. If there are outstandingPermitorOwnedPermitvalues, therecvmethod will not returnNoneuntil those are released.Examples
use mpsc; asyncfn is_closed(self: &Self) -> boolChecks if a channel is closed.
This method returns
trueif the channel has been closed. The channel is closed when allSenderhave been dropped, or whenReceiver::closeis called.Examples
use mpsc; asyncfn is_empty(self: &Self) -> boolChecks if a channel is empty.
This method returns
trueif the channel has no messages.Examples
use mpsc; asyncfn len(self: &Self) -> usizeReturns the number of messages in the channel.
Examples
use mpsc; asyncfn capacity(self: &Self) -> usizeReturns the current capacity of the channel.
The capacity goes down when the sender sends a value by calling
Sender::sendor by reserving capacity withSender::reserve. The capacity goes up when values are received. This is distinct frommax_capacity, which always returns buffer capacity initially specified when callingchannel.Examples
use mpsc; asyncfn max_capacity(self: &Self) -> usizeReturns the maximum buffer capacity of the channel.
The maximum capacity is the buffer capacity initially specified when calling
channel. This is distinct fromcapacity, which returns the current available buffer capacity: as messages are sent and received, the value returned bycapacitywill go up or down, whereas the value returned bymax_capacitywill remain constant.Examples
use mpsc; asyncfn poll_recv(self: &mut Self, cx: &mut Context<'_>) -> Poll<Option<T>>Polls to receive the next message on this channel.
This method returns:
Poll::Pendingif no messages are available but the channel is not closed, or if a spurious failure happens.Poll::Ready(Some(message))if a message is available.Poll::Ready(None)if the channel has been closed and all messages sent before it was closed have been received.
When the method returns
Poll::Pending, theWakerin the providedContextis scheduled to receive a wakeup when a message is sent on any receiver, or when the channel is closed. Note that on multiple calls topoll_recvorpoll_recv_many, only theWakerfrom theContextpassed to the most recent call is scheduled to receive a wakeup.If this method returns
Poll::Pendingdue to a spurious failure, then theWakerwill be notified when the situation causing the spurious failure has been resolved. Note that receiving such a wakeup does not guarantee that the next call will succeed — it could fail with another spurious failure.fn poll_recv_many(self: &mut Self, cx: &mut Context<'_>, buffer: &mut Vec<T>, limit: usize) -> Poll<usize>Polls to receive multiple messages on this channel, extending the provided buffer.
This method returns:
Poll::Pendingif no messages are available but the channel is not closed, or if a spurious failure happens.Poll::Ready(count)wherecountis the number of messages successfully received and stored inbuffer. This can be less than, or equal to,limit.Poll::Ready(0)iflimitis set to zero or when the channel is closed.
When the method returns
Poll::Pending, theWakerin the providedContextis scheduled to receive a wakeup when a message is sent on any receiver, or when the channel is closed. Note that on multiple calls topoll_recvorpoll_recv_many, only theWakerfrom theContextpassed to the most recent call is scheduled to receive a wakeup.Note that this method does not guarantee that exactly
limitmessages are received. Rather, if at least one message is available, it returns as many messages as it can up to the given limit. This method returns zero only if the channel is closed (or iflimitis zero).Examples
use ; use Pin; use mpsc; use Future; asyncfn sender_strong_count(self: &Self) -> usizeReturns the number of
Senderhandles.fn sender_weak_count(self: &Self) -> usizeReturns the number of
WeakSenderhandles.
impl<T> Any for Receiver<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Receiver<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Receiver<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> Debug for Receiver<T>
fn fmt(self: &Self, fmt: &mut Formatter<'_>) -> Result
impl<T> Freeze for Receiver<T>
impl<T> From for Receiver<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> RefUnwindSafe for Receiver<T>
impl<T> Send for Receiver<T>
impl<T> Sync for Receiver<T>
impl<T> Unpin for Receiver<T>
impl<T> UnsafeUnpin for Receiver<T>
impl<T> UnwindSafe for Receiver<T>
impl<T, U> Into for Receiver<T>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Receiver<T>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Receiver<T>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>