Skip to content

Commit

Permalink
Ch. 17§04 initial edits on Building Our Own Async Abstractions
Browse files Browse the repository at this point in the history
Eliminate `trpl::timeout` entirely. Instead, just build the `timeout`
directly (as the rest of the section already did). Restructure the
listings as well, eliminating duplication and extraneous bits.
  • Loading branch information
chriskrycho committed Jul 9, 2024
1 parent 1030015 commit ef1ed96
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 713 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ fn main() {
trpl::block_on(async {
// ANCHOR: here
let slow = async {
trpl::sleep(Duration::from_secs(5)).await;
trpl::sleep(Duration::from_millis(100)).await;
"I finished!"
};

match trpl::timeout(Duration::from_secs(2), slow).await {
match timeout(slow, Duration::from_millis(10)).await {
Ok(message) => println!("Succeeded with '{message}'"),
Err(duration) => {
println!("Failed after {} seconds", duration.as_secs())
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ fn main() {
"Finally finished"
};

// Here we will actually use the new `timeout` with `slow`.
match timeout(slow, Duration::from_millis(10)).await {
Ok(message) => println!("Succeeded with '{message}'"),
Err(duration) => {
println!("Failed after {} seconds", duration.as_secs())
}
}
});
}

// ANCHOR: declaration
async fn timeout<F: Future>(
max_time: Duration,
future: F,
max_time: Duration,
) -> Result<F::Output, Duration> {
// ANCHOR_END: declaration
unimplemented!()
// Here is where our implementation will go!
}
// ANCHOR_END: declaration

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ fn main() {
"Finally finished"
};

// ANCHOR: main
match timeout(Duration::from_secs(2), slow).await {
// ANCHOR_END: main
Ok(message) => println!("Succeeded with '{message}'"),
Err(duration) => {
println!("Failed after {} seconds", duration.as_secs())
Expand All @@ -24,8 +22,10 @@ async fn timeout<F: Future>(
max_time: Duration,
future: F,
) -> Result<F::Output, Duration> {
// ANCHOR: implementation
match trpl::race(future, trpl::sleep(max_time)).await {
Either::Left(output) => Ok(output),
Either::Right(_) => Err(max_time),
}
// ANCHOR_END: implementation
}
Loading

0 comments on commit ef1ed96

Please sign in to comment.