Skip to content

Commit

Permalink
Clarify the suggested steps in the pin page (#2130)
Browse files Browse the repository at this point in the history
The speaker notes suggest an evolution of the code to support a periodic
timer, but the last step was under-specified.

(As mentioned by @fw-immunant and referenced in #1536)
  • Loading branch information
djmitche authored Jun 7, 2024
1 parent b69b68f commit 412eac6
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/concurrency/async-pitfalls/pin.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async fn main() {
- Instead, add a `timeout_fut` containing that future outside of the `loop`:

```rust,compile_fail
let mut timeout_fut = sleep(Duration::from_millis(100));
let timeout_fut = sleep(Duration::from_millis(100));
loop {
select! {
..,
Expand All @@ -106,7 +106,18 @@ async fn main() {
- This compiles, but once the timeout expires it is `Poll::Ready` on every
iteration (a fused future would help with this). Update to reset
`timeout_fut` every time it expires.
`timeout_fut` every time it expires:
```rust,compile_fail
let mut timeout_fut = Box::pin(sleep(Duration::from_millis(100)));
loop {
select! {
_ = &mut timeout_fut => {
println!(..);
timeout_fut = Box::pin(sleep(Duration::from_millis(100)));
},
}
}
```
- Box allocates on the heap. In some cases, `std::pin::pin!` (only recently
stabilized, with older code often using `tokio::pin!`) is also an option, but
Expand Down

0 comments on commit 412eac6

Please sign in to comment.