use core::{
future::Future,
pin::Pin,
ptr::null,
task::{Poll, RawWaker, RawWakerVTable, Waker},
};
pub async fn yield_now() {
YieldNow::Pending.await
}
enum YieldNow {
Pending,
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(()),
}
}
}
#[inline]
pub(crate) fn noop_waker() -> Waker {
unsafe { Waker::from_raw(noop_raw_waker()) }
}
const NOOP_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(noop_clone, noop, noop, noop);
const unsafe fn noop_clone(_data: *const ()) -> RawWaker {
noop_raw_waker()
}
const unsafe fn noop(_data: *const ()) {}
const fn noop_raw_waker() -> RawWaker {
RawWaker::new(null(), &NOOP_WAKER_VTABLE)
}