-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
900 additions
and
7 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 |
---|---|---|
|
@@ -64,6 +64,7 @@ proxying | |
pseudocode | ||
ReadIntoBuf | ||
recognise | ||
repo | ||
refactor | ||
RefCell | ||
repo | ||
|
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
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,8 @@ | ||
[package] | ||
name = "hello-world-join" | ||
version = "0.1.0" | ||
authors = ["Nicholas Cameron <[email protected]>"] | ||
edition = "2021" | ||
|
||
[dependencies] | ||
tokio = { version = "1.40.0", features = ["full"] } |
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,23 @@ | ||
use tokio::{spawn, time::{sleep, Duration}}; | ||
|
||
async fn say_hello() { | ||
// Wait for a while before printing to make it a more interesting race. | ||
sleep(Duration::from_millis(100)).await; | ||
println!("hello"); | ||
} | ||
|
||
async fn say_world() { | ||
sleep(Duration::from_millis(100)).await; | ||
println!("world"); | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let handle1 = spawn(say_hello()); | ||
let handle2 = spawn(say_world()); | ||
|
||
let _ = handle1.await; | ||
let _ = handle2.await; | ||
|
||
println!("!"); | ||
} |
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,8 @@ | ||
[package] | ||
name = "hello-world-sleep" | ||
version = "0.1.0" | ||
authors = ["Nicholas Cameron <[email protected]>"] | ||
edition = "2021" | ||
|
||
[dependencies] | ||
tokio = { version = "1.40.0", features = ["full"] } |
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,20 @@ | ||
use std::io::{stdout, Write}; | ||
use tokio::time::{sleep, Duration}; | ||
|
||
async fn say_hello() { | ||
print!("hello, "); | ||
// Flush stdout so we see the effect of the above `print` immediately. | ||
stdout().flush().unwrap(); | ||
} | ||
|
||
async fn say_world() { | ||
println!("world!"); | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
say_hello().await; | ||
// An async sleep function, puts the current task to sleep for 1s. | ||
sleep(Duration::from_millis(1000)).await; | ||
say_world().await; | ||
} |
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,8 @@ | ||
[package] | ||
name = "hello-world-spawn" | ||
version = "0.1.0" | ||
authors = ["Nicholas Cameron <[email protected]>"] | ||
edition = "2021" | ||
|
||
[dependencies] | ||
tokio = { version = "1.40.0", features = ["full"] } |
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,20 @@ | ||
use tokio::{spawn, time::{sleep, Duration}}; | ||
|
||
async fn say_hello() { | ||
// Wait for a while before printing to make it a more interesting race. | ||
sleep(Duration::from_millis(100)).await; | ||
println!("hello"); | ||
} | ||
|
||
async fn say_world() { | ||
sleep(Duration::from_millis(100)).await; | ||
println!("world!"); | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
spawn(say_hello()); | ||
spawn(say_world()); | ||
// Wait for a while to give the tasks time to run. | ||
sleep(Duration::from_millis(1000)).await; | ||
} |
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
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
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,65 @@ | ||
# More async/await topics | ||
|
||
## Unit tests | ||
|
||
## Blocking and cancellation | ||
|
||
- Two important concepts to be aware of early, we'll revisit in more detail as we go along | ||
- Cancellation | ||
- How to do it | ||
- drop a future | ||
- cancellation token | ||
- abort functions | ||
- Why it matters, cancellation safety (forward ref) | ||
- Blocking | ||
- IO and computation can block | ||
- why it's bad | ||
- how to deal is a forward ref to io chapter | ||
|
||
## `Send + 'static` bounds on futures | ||
|
||
- Why they're there, multi-threaded runtimes | ||
- spawn local to avoid them | ||
- What makes an async fn `Send + 'static` and how to fix bugs with it | ||
|
||
## Async traits | ||
|
||
- syntax | ||
- The `Send + 'static` issue and working around it | ||
- trait_variant | ||
- explicit future | ||
- return type notation (https://blog.rust-lang.org/inside-rust/2024/09/26/rtn-call-for-testing.html) | ||
- overriding | ||
- future vs async notation for methods | ||
- object safety | ||
- capture rules (https://blog.rust-lang.org/2024/09/05/impl-trait-capture-rules.html) | ||
- history and async-trait crate | ||
|
||
|
||
## Async blocks and closures | ||
|
||
- async block syntax | ||
- what it means | ||
- using an async block in a function returning a future | ||
- subtype of async method | ||
- closures | ||
- coming soon (https://github.com/rust-lang/rust/pull/132706, https://blog.rust-lang.org/inside-rust/2024/08/09/async-closures-call-for-testing.html) | ||
- async blocks in closures vs async closures | ||
- errors in async blocks | ||
- https://rust-lang.github.io/async-book/07_workarounds/02_err_in_async_blocks.html | ||
|
||
## Recursion | ||
|
||
- Allowed (relatively new), but requires some explicit boxing | ||
- forward reference to futures, pinning | ||
- https://rust-lang.github.io/async-book/07_workarounds/04_recursion.html | ||
- https://blog.rust-lang.org/2024/03/21/Rust-1.77.0.html#support-for-recursion-in-async-fn | ||
- async-recursion macro (https://docs.rs/async-recursion/latest/async_recursion/) | ||
|
||
|
||
## Lifetimes and borrowing | ||
|
||
- Mentioned the static lifetime above | ||
- Lifetime bounds on futures (`Future + '_`, etc.) | ||
- Borrowing across await points | ||
- I don't know, I'm sure there are more lifetime issues with async functions ... |
Oops, something went wrong.