Skip to content

Commit

Permalink
block-on: Add example from Chapter 20, Asynchronous Programming.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblandy committed Jun 14, 2021
1 parent 083e5ec commit e4470d3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions block-on/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target/
Cargo.lock
15 changes: 15 additions & 0 deletions block-on/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "block-on"
version = "0.1.0"
authors = ["You <[email protected]>"]
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"
44 changes: 44 additions & 0 deletions block-on/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit e4470d3

Please sign in to comment.