Struct Receiver
struct Receiver<T> { ... }
Receives a value from the associated Sender.
A pair of both a Sender and a Receiver are created by the
channel function.
This channel has no recv method because the receiver itself implements the
Future trait. To receive a Result<T, [error::RecvError]>, .await the Receiver object directly.
The poll method on the Future trait is allowed to spuriously return
Poll::Pending even if the message has been sent. If such a spurious
failure happens, then the caller will be woken when the spurious failure has
been resolved so that the caller can attempt to receive the message again.
Note that receiving such a wakeup does not guarantee that the next call will
succeed — it could fail with another spurious failure. (A spurious failure
does not mean that the message is lost. It is just delayed.)
Examples
use oneshot;
async
If the sender is dropped without sending, the receiver will fail with
[error::RecvError]:
use oneshot;
async
To use a Receiver in a tokio::select! loop, add &mut in front of the
channel.
use oneshot;
use ;
# async
#
async
Implementations
impl<T> Receiver<T>
fn close(self: &mut Self)Prevents the associated
Senderhandle from sending a value.Any
sendoperation which happens after callingcloseis guaranteed to fail. After callingclose,try_recvshould be called to receive a value if one was sent before the call toclosecompleted.This function is useful to perform a graceful shutdown and ensure that a value will not be sent into the channel and never received.
closeis no-op if a message is already received or the channel is already closed.Examples
Prevent a value from being sent
use oneshot; use TryRecvError; asyncReceive a value sent before calling
closeuse oneshot; asyncfn is_terminated(self: &Self) -> boolChecks if this receiver is terminated.
This function returns true if this receiver has already yielded a
Poll::Readyresult. If so, this receiver should no longer be polled.Examples
Sending a value and polling it.
use oneshot; use Poll; asyncDropping the sender.
use oneshot; asyncfn is_empty(self: &Self) -> boolChecks if a channel is empty.
This method returns
trueif the channel has no messages.It is not necessarily safe to poll an empty receiver, which may have already yielded a value. Use [
is_terminated()][Self::is_terminated] to check whether or not a receiver can be safely polled, instead.Examples
Sending a value.
use oneshot; asyncDropping the sender.
use oneshot; asyncTerminated channels are empty.
use tokio::sync::oneshot; #[tokio::main] async fn main() { let (tx, mut rx) = oneshot::channel(); tx.send(0).unwrap(); let _ = (&mut rx).await; // NB: an empty channel is not necessarily safe to poll! assert!(rx.is_empty()); let _ = (&mut rx).await; }fn try_recv(self: &mut Self) -> Result<T, TryRecvError>Attempts to receive a value.
If a pending value exists in the channel, it is returned. If no value has been sent, the current task will not be registered for future notification.
This function is useful to call from outside the context of an asynchronous task.
Note that unlike the
pollmethod, thetry_recvmethod cannot fail spuriously. Any send or close event that happens before this call totry_recvwill be correctly returned to the caller.Return
Ok(T)if a value is pending in the channel.Err(TryRecvError::Empty)if no value has been sent yet.Err(TryRecvError::Closed)if the sender has dropped without sending a value, or if the message has already been received.
Examples
try_recvbefore a value is sent, then after.use oneshot; use TryRecvError; asynctry_recvwhen the sender dropped before sending a valueuse oneshot; use TryRecvError; asyncfn blocking_recv(self: Self) -> Result<T, RecvError>Blocking receive to call outside of asynchronous contexts.
Panics
This function panics if called within an asynchronous execution context.
Examples
use thread; use oneshot; async
impl<F> IntoFuture for Receiver<T>
fn into_future(self: Self) -> <F as IntoFuture>::IntoFuture
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> Drop for Receiver<T>
fn drop(self: &mut Self)
impl<T> Freeze for Receiver<T>
impl<T> From for Receiver<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> Future for Receiver<T>
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<<Self as >::Output>
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> 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>
impl<T: $crate::fmt::Debug> Debug for Receiver<T>
fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result