Struct Receiver
pub struct Receiver<T> { /* private fields */ }
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.)
Cancel safety
Awaiting a &mut Receiver<T> is cancel safe. If it is used as a branch in
tokio::select! and another branch completes first, it is
guaranteed that no message was received on this
channel.
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(&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) -> 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) -> 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(&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) -> Result<T, RecvError>Blocking receive to call outside of asynchronous contexts.
Panics
This function panics if called within an asynchronous execution context.
Examples
# #
Trait Implementations
impl<T> Drop for Receiver<T>
fn drop(&mut self)
impl<T> Future for Receiver<T>
type Output = Result<T, RecvError>;fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>
impl<T: Debug> Debug for Receiver<T>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Auto Trait Implementations
impl<T> !RefUnwindSafe for Receiver<T>
impl<T> !UnwindSafe for Receiver<T>
impl<T> Freeze for Receiver<T>
where
Option<Arc<Inner<T>>>: Freeze,
impl<T> Send for Receiver<T>
where
Option<Arc<Inner<T>>>: Send,
impl<T> Sync for Receiver<T>
where
Option<Arc<Inner<T>>>: Sync,
impl<T> Unpin for Receiver<T>
where
Option<Arc<Inner<T>>>: Unpin,
impl<T> UnsafeUnpin for Receiver<T>
where
Option<Arc<Inner<T>>>: UnsafeUnpin,
Blanket Implementations
impl<F> IntoFuture for Receiver<T>
where
F: Future,
type Output = <F as Future>::Output;type IntoFuture = F;fn into_future(self) -> <F as IntoFuture>::IntoFuture
impl<T> Any for Receiver<T>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Receiver<T>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Receiver<T>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for Receiver<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for Receiver<T>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Receiver<T>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Receiver<T>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>