The tokio tutorial for select! states:
The thing to note is that, to .await a reference, the value being referenced must be pinned or implement Unpin.
Indeed, the following code fails to compile:
let fut = example(); // example is an async fn
(&mut fut).await;
With the following error message:
error[E0277]: `from_generator::GenFuture<[static generator@src/main.rs:15:27: 17:2]>` cannot be unpinned
... snip ...
within `impl futures::Future<Output = i32>`, the trait `Unpin` is not implemented for `from_generator::GenFuture<[static generator@src/main.rs:15:27: 17:2]>
... snip ...
note: consider using `Box::pin`
Pinning the future solves the problem:
let fut = example(); // example is an async fn
tokio::pin!(fut);
(&mut fut).await;
Why is it necessary to pin the future in order to await a reference to it?