[−][src]Struct tokio::sync::oneshot::Sender
Sends a value to the associated Receiver.
Instances are created by the channel function.
Methods
impl<T> Sender<T>[src]
pub fn send(self, t: T) -> Result<(), T>[src]
Attempts to send a value on this channel, returning it back if it could not be sent.
The function consumes self as only one value may ever be sent on a
one-shot channel.
A successful send occurs when it is determined that the other end of the
channel has not hung up already. An unsuccessful send would be one where
the corresponding receiver has already been deallocated. Note that a
return value of Err means that the data will never be received, but
a return value of Ok does not mean that the data will be received.
It is possible for the corresponding receiver to hang up immediately
after this function returns Ok.
Examples
Send a value to another task
use tokio::sync::oneshot; #[tokio::main] async fn main() { let (tx, rx) = oneshot::channel(); tokio::spawn(async move { if let Err(_) = tx.send(3) { println!("the receiver dropped"); } }); match rx.await { Ok(v) => println!("got = {:?}", v), Err(_) => println!("the sender dropped"), } }
pub async fn closed<'_>(&'_ mut self)[src]
Waits for the associated Receiver handle to close.
A Receiver is closed by either calling close explicitly or the
Receiver value is dropped.
This function is useful when paired with select! to abort a
computation when the receiver is no longer interested in the result.
Return
Returns a Future which must be awaited on.
Examples
Basic usage
use tokio::sync::oneshot; #[tokio::main] async fn main() { let (mut tx, rx) = oneshot::channel::<()>(); tokio::spawn(async move { drop(rx); }); tx.closed().await; println!("the receiver dropped"); }
Paired with select
use tokio::sync::oneshot; use tokio::time::{self, Duration}; use futures::{select, FutureExt}; async fn compute() -> String { // Complex computation returning a `String` } #[tokio::main] async fn main() { let (mut tx, rx) = oneshot::channel(); tokio::spawn(async move { select! { _ = tx.closed().fuse() => { // The receiver dropped, no need to do any further work } value = compute().fuse() => { tx.send(value).unwrap() } } }); // Wait for up to 10 seconds let _ = time::timeout(Duration::from_secs(10), rx).await; }
pub fn is_closed(&self) -> bool[src]
Returns true if the associated Receiver handle has been dropped.
A Receiver is closed by either calling close explicitly or the
Receiver value is dropped.
If true is returned, a call to send will always result in an error.
Examples
use tokio::sync::oneshot; #[tokio::main] async fn main() { let (tx, rx) = oneshot::channel(); assert!(!tx.is_closed()); drop(rx); assert!(tx.is_closed()); assert!(tx.send("never received").is_err()); }
Trait Implementations
Auto Trait Implementations
impl<T> !RefUnwindSafe for Sender<T>
impl<T> Send for Sender<T> where
T: Send,
T: Send,
impl<T> Sync for Sender<T> where
T: Send,
T: Send,
impl<T> Unpin for Sender<T>
impl<T> !UnwindSafe for Sender<T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized, [src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized, [src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
T: ?Sized,
ⓘImportant traits for &'_ mut Ffn borrow_mut(&mut self) -> &mut T[src]
impl<T> From<T> for T[src]
impl<T, U> Into<U> for T where
U: From<T>, [src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>, [src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>, [src]
U: TryFrom<T>,