codas_flow/
async_support.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//! Runtime-agnostic `async` utilities.

use core::{future::Future, pin::Pin, task::Poll};

/// Returns a future that becomes ready
/// after one poll cycle, emulating a yield
/// on most async runtimes.
pub fn yield_now() -> impl Future<Output = ()> {
    YieldNow::Pending
}

/// Future returned by [`yield_now`].
enum YieldNow {
    /// The future has not yet yielded.
    Pending,

    /// The future has yielded for
    /// at least one poll cycle, and
    /// is now ready.
    Ready,
}

impl Future for YieldNow {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, cx: &mut core::task::Context<'_>) -> Poll<Self::Output> {
        match *self {
            YieldNow::Pending => {
                *self = YieldNow::Ready;
                cx.waker().wake_by_ref();
                Poll::Pending
            }
            YieldNow::Ready => Poll::Ready(()),
        }
    }
}