Function futures_channel::oneshot::channel
source · pub fn channel<T>() -> (Sender<T>, Receiver<T>)
Expand description
Creates a new one-shot channel for sending a single value across asynchronous tasks.
The channel works for a spsc (single-producer, single-consumer) scheme.
This function is similar to Rust’s channel constructor found in the standard
library. Two halves are returned, the first of which is a Sender
handle,
used to signal the end of a computation and provide its value. The second
half is a Receiver
which implements the Future
trait, resolving to the
value that was given to the Sender
handle.
Each half can be separately owned and sent across tasks.
§Examples
use futures::channel::oneshot;
use std::{thread, time::Duration};
let (sender, receiver) = oneshot::channel::<i32>();
thread::spawn(|| {
println!("THREAD: sleeping zzz...");
thread::sleep(Duration::from_millis(1000));
println!("THREAD: i'm awake! sending.");
sender.send(3).unwrap();
});
println!("MAIN: doing some useful stuff");
futures::executor::block_on(async {
println!("MAIN: waiting for msg...");
println!("MAIN: got: {:?}", receiver.await)
});