block-on: Add example from Chapter 20, Asynchronous Programming.
parent
083e5ec83a
commit
e4470d30b0
@ -0,0 +1,2 @@
|
||||
/target/
|
||||
Cargo.lock
|
@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "block-on"
|
||||
version = "0.1.0"
|
||||
authors = ["You <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
waker-fn = "1.1"
|
||||
futures-lite = "1.11"
|
||||
crossbeam = "0.8"
|
||||
|
||||
[dev-dependencies]
|
||||
async-std = "1.7"
|
@ -0,0 +1,44 @@
|
||||
use waker_fn::waker_fn; // Cargo.toml: waker-fn = "1.1"
|
||||
use futures_lite::pin; // Cargo.toml: futures-lite = "1.11"
|
||||
use crossbeam::sync::Parker; // Cargo.toml: crossbeam = "0.8"
|
||||
use std::future::Future;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
pub fn block_on<F: Future>(future: F) -> F::Output {
|
||||
let parker = Parker::new();
|
||||
let unparker = parker.unparker().clone();
|
||||
let waker = waker_fn(move || unparker.unpark());
|
||||
let mut context = Context::from_waker(&waker);
|
||||
|
||||
pin!(future);
|
||||
|
||||
loop {
|
||||
match future.as_mut().poll(&mut context) {
|
||||
Poll::Ready(value) => return value,
|
||||
Poll::Pending => parker.park(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
assert_eq!(block_on(std::future::ready(42)), 42);
|
||||
|
||||
use async_std::task::{spawn, sleep};
|
||||
use futures_lite::FutureExt;
|
||||
use std::time::Duration;
|
||||
|
||||
assert_eq!(
|
||||
block_on({
|
||||
let one_sec = async {
|
||||
sleep(Duration::from_secs(1)).await;
|
||||
43
|
||||
};
|
||||
let half_sec = async {
|
||||
sleep(Duration::from_millis(500)).await;
|
||||
44
|
||||
};
|
||||
spawn(one_sec.race(half_sec))
|
||||
}),
|
||||
44);
|
||||
}
|
Loading…
Reference in New Issue