forked from ProgrammingRust/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
block-on: Add example from Chapter 20, Asynchronous Programming.
- Loading branch information
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target/ | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |