codas_flow/async_support.rs
//! 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(()),
}
}
}