mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-09 02:56:15 +02:00
Clarify the suggested steps in the pin page (#2130)
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)
This commit is contained in:
parent
b69b68f5e2
commit
412eac6689
@ -82,7 +82,7 @@ async fn main() {
|
|||||||
- Instead, add a `timeout_fut` containing that future outside of the `loop`:
|
- Instead, add a `timeout_fut` containing that future outside of the `loop`:
|
||||||
|
|
||||||
```rust,compile_fail
|
```rust,compile_fail
|
||||||
let mut timeout_fut = sleep(Duration::from_millis(100));
|
let timeout_fut = sleep(Duration::from_millis(100));
|
||||||
loop {
|
loop {
|
||||||
select! {
|
select! {
|
||||||
..,
|
..,
|
||||||
@ -106,7 +106,18 @@ async fn main() {
|
|||||||
|
|
||||||
- This compiles, but once the timeout expires it is `Poll::Ready` on every
|
- This compiles, but once the timeout expires it is `Poll::Ready` on every
|
||||||
iteration (a fused future would help with this). Update to reset
|
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
|
- 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
|
stabilized, with older code often using `tokio::pin!`) is also an option, but
|
||||||
|
Loading…
x
Reference in New Issue
Block a user