#![allow(unused)]fnmain() {
use std::pin::Pin;
use std::future::Future;
use std::task::{Context, Poll};
structWakeOnceThenComplete(bool);
fnwake_and_yield_once() -> WakeOnceThenComplete {
WakeOnceThenComplete(false)
}
impl Future for WakeOnceThenComplete {
typeOutput = ();
fnpoll(mutself: Pin<&mutSelf>, cx: &mut Context<'_>) -> Poll<()> {
ifself.0 {
Poll::Ready(())
} else {
cx.waker().wake_by_ref();
self.0 = true;
Poll::Pending
}
}
}
fnfoo() {
wake_and_yield_once().await// `await` is used outside `async` context
}
}
ⓘ
await is used to suspend the current computation until the given
future is ready to produce a value. So it is legal only within
an async context, like an async function or an async block.
#![allow(unused)]fnmain() {
use std::pin::Pin;
use std::future::Future;
use std::task::{Context, Poll};
structWakeOnceThenComplete(bool);
fnwake_and_yield_once() -> WakeOnceThenComplete {
WakeOnceThenComplete(false)
}
impl Future for WakeOnceThenComplete {
typeOutput = ();
fnpoll(mutself: Pin<&mutSelf>, cx: &mut Context<'_>) -> Poll<()> {
ifself.0 {
Poll::Ready(())
} else {
cx.waker().wake_by_ref();
self.0 = true;
Poll::Pending
}
}
}
asyncfnfoo() {
wake_and_yield_once().await// `await` is used within `async` function
}
fnbar(x: u8) -> impl Future<Output = u8> {
asyncmove {
wake_and_yield_once().await; // `await` is used within `async` block
x
}
}
}