There are some general Future
trait implementations in the futures crate. One of them is implemented in futures::sync::oneshot
module and is available through futures::oneshot
function:
extern crate futures;
use std::thread;
use futures::Future;
fn expensive_computation() -> u32 {
// ...
200
}
fn main() {
// The oneshot function returns a tuple of a Sender and a Receiver.
let (tx, rx) = futures::oneshot();
thread::spawn(move || {
// The complete method resolves a values.
tx.complete(expensive_computation());
});
// The map method applies a function to a value, when it is resolved.
let rx = rx.map(|x| {
println!("{}", x);
});
// The wait method blocks current thread until the value is resolved.
rx.wait().unwrap();
}