use core::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
#[derive(Debug)]
#[must_use = "futures do nothing unless `.await`ed or polled"]
pub struct Yield {
yields: usize,
}
impl Future for Yield {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
let yields = &mut self.as_mut().yields;
if *yields == 0 {
return Poll::Ready(());
}
*yields -= 1;
cx.waker().wake_by_ref();
Poll::Pending
}
}
impl Yield {
#[inline]
pub const fn new(yields: usize) -> Self {
Self { yields }
}
}
#[inline]
pub fn yield_now() -> Yield {
Yield::new(1)
}