wasmtime/runtime/vm/
async_yield.rs

1use core::{
2    future::Future,
3    pin::Pin,
4    task::{Context, Poll},
5};
6
7/// A small future that yields once and then returns.
8#[derive(Default)]
9pub struct Yield {
10    yielded: bool,
11}
12
13impl Yield {
14    /// Create a new `Yield`.
15    pub fn new() -> Self {
16        Self::default()
17    }
18}
19
20impl Future for Yield {
21    type Output = ();
22
23    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
24        if self.yielded {
25            Poll::Ready(())
26        } else {
27            // Flag ourselves as yielded to return next time, and also
28            // flag the waker that we're already ready to get
29            // re-enqueued for another poll.
30            self.yielded = true;
31            cx.waker().wake_by_ref();
32            Poll::Pending
33        }
34    }
35}