Skip to content

Commit 9d5daf8

Browse files
committed
wip
1 parent c4958b6 commit 9d5daf8

File tree

10 files changed

+24
-26
lines changed

10 files changed

+24
-26
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ members = [
99
"src/borrowing",
1010
"src/control-flow-basics",
1111
"src/error-handling",
12+
"src/concurrency",
1213
"src/concurrency/sync-exercises",
1314
"src/concurrency/async-exercises",
1415
"src/concurrency/async-exercises/chat-async",

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@
387387
- [Futures](concurrency/async/futures.md)
388388
- [Runtimes](concurrency/async/runtimes.md)
389389
- [Tokio](concurrency/async/runtimes/tokio.md)
390-
- [Tasks](concurrency/async/tasks.md)
390+
<!-- - [Tasks](concurrency/async/tasks.md) -->
391391
- [Channels and Control Flow](concurrency/async-control-flow.md)
392392
- [Async Channels](concurrency/async-control-flow/channels.md)
393393
- [Join](concurrency/async-control-flow/join.md)

src/concurrency/async-control-flow/channels.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ minutes: 8
44

55
# Async Channels
66

7-
Several crates have support for asynchronous channels. For instance `tokio`:
7+
Asynchronous channels are very similar to synchronous channels:
88

99
```rust,editable,compile_fail
1010
{{#include channels.rs}}

src/concurrency/async-control-flow/channels.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ use tokio::sync::mpsc;
22

33
async fn ping_handler(mut input: mpsc::Receiver<()>) {
44
let mut count = 0;
5-
65
while let Some(_) = input.recv().await {
76
count += 1;
87
println!("Received {count} pings so far.");
98
}
10-
119
println!("ping_handler complete");
1210
}
1311

src/concurrency/async-exercises/chat-app.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ You are going to need the following functions from `tokio` and
2929
[`tokio_websockets`][2]. Spend a few minutes to familiarize yourself with the
3030
API.
3131

32-
- [StreamExt::next()][3] implemented by `WebSocketStream`: for asynchronously
32+
- [`StreamExt::next()`][3] implemented by `WebSocketStream`: for asynchronously
3333
reading messages from a Websocket Stream.
34-
- [SinkExt::send()][4] implemented by `WebSocketStream`: for asynchronously
34+
- [`SinkExt::send()`][4] implemented by `WebSocketStream`: for asynchronously
3535
sending messages on a Websocket Stream.
36-
- [Lines::next_line()][5]: for asynchronously reading user messages from the
36+
- [`Lines::next_line()`][5]: for asynchronously reading user messages from the
3737
standard input.
38-
- [Sender::subscribe()][6]: for subscribing to a broadcast channel.
38+
- [`Sender::subscribe()`][6]: for subscribing to a broadcast channel.
3939

4040
## Two binaries
4141

@@ -58,9 +58,8 @@ _src/bin/server.rs_:
5858
{{#include chat-async/src/bin/server.rs:setup}}
5959
6060
{{#include chat-async/src/bin/server.rs:handle_connection}}
61-
6261
// TODO: For a hint, see the description of the task below.
63-
62+
Ok(())
6463
{{#include chat-async/src/bin/server.rs:main}}
6564
```
6665

@@ -72,7 +71,7 @@ _src/bin/client.rs_:
7271
{{#include chat-async/src/bin/client.rs:setup}}
7372
7473
// TODO: For a hint, see the description of the task below.
75-
74+
Ok(())
7675
}
7776
```
7877

src/concurrency/async-exercises/dining-philosophers.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@ impl Philosopher {
4444
// Keep trying until we have both forks
4545
// ANCHOR_END: Philosopher-eat
4646
let (_left_fork, _right_fork) = loop {
47-
tokio::task::yield_now().await;
4847
// Pick up forks...
4948
match (self.left_fork.try_lock(), self.right_fork.try_lock()) {
50-
(Ok(left_fork), Ok(right_fork)) => (left_fork, right_fork),
49+
(Ok(left_fork), Ok(right_fork)) => break (left_fork, right_fork),
5150
(_, _) => continue,
5251
};
53-
break (left_fork, right_fork);
5452
};
5553

5654
// ANCHOR: Philosopher-eat-body

src/concurrency/async-pitfalls/async-traits.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ minutes: 5
44

55
# Async Traits
66

7-
Async methods in traits are were stabilized in the 1.75 release. This required
8-
support for using return-position `impl Trait` in traits, as the desugaring for
9-
`async fn` includes `-> impl Future<Output = ...>`.
7+
Async methods in traits are were stabilized in the 1.75 release (December 2023).
8+
This required support for using return-position `impl Trait` in traits, as the
9+
desugaring for `async fn` includes `-> impl Future<Output = ...>`.
1010

1111
However, even with the native support, there are some pitfalls around
1212
`async fn`:

src/concurrency/async-pitfalls/async-traits.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use async_trait::async_trait;
2-
use std::time::Instant;
32
use tokio::time::{sleep, Duration};
43

54
#[async_trait]
@@ -18,14 +17,11 @@ impl Sleeper for FixedSleeper {
1817
}
1918
}
2019

21-
async fn run_all_sleepers_multiple_times(
22-
sleepers: Vec<Box<dyn Sleeper>>,
23-
n_times: usize,
24-
) {
25-
for _ in 0..n_times {
20+
async fn run_all_sleepers_multiple_times(sleepers: Vec<Box<dyn Sleeper>>) {
21+
for _ in 0..5 {
2622
println!("Running all sleepers...");
2723
for sleeper in &sleepers {
28-
let start = Instant::now();
24+
let start = std::time::Instant::now();
2925
sleeper.sleep().await;
3026
println!("Slept for {} ms", start.elapsed().as_millis());
3127
}
@@ -38,5 +34,5 @@ async fn main() {
3834
Box::new(FixedSleeper { sleep_ms: 50 }),
3935
Box::new(FixedSleeper { sleep_ms: 100 }),
4036
];
41-
run_all_sleepers_multiple_times(sleepers, 5).await;
37+
run_all_sleepers_multiple_times(sleepers).await;
4238
}

src/concurrency/async-pitfalls/pin.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ minutes: 20
55
# `Pin`
66

77
Async blocks and functions return types implementing the `Future` trait. The
8-
type returned is the result of a compiler transformation which turns local
8+
type returned is the result of a [compiler transformation] which turns local
99
variables into data stored inside the future.
1010

11+
[compiler transformation]: https://tokio.rs/tokio/tutorial/async
12+
1113
Some of those variables can hold pointers to other local variables. Because of
1214
that, the future should never be moved to a different memory location, as it
1315
would invalidate those pointers.

0 commit comments

Comments
 (0)