use core::pin::Pin;
use core::task::{Context, Poll};
pub const YIELD: _Yield = _Yield(false);
#[deprecated(since = "0.2.5", note = "Use `Yield.await` instead")]
pub async fn yield_now() { _Yield(false).await }
#[derive(Debug)]
pub struct Yield;
impl IntoFuture for Yield {
type Output = ();
type IntoFuture = _Yield;
fn into_future(self) -> Self::IntoFuture { _Yield(false) }
}
#[derive(Debug)]
pub struct _Yield(bool);
impl Future for _Yield {
type Output = ();
fn poll(mut self: Pin<&mut Self>, _: &mut Context) -> Poll<Self::Output> {
if self.0 { Poll::Ready(()) } else { self.0 = true; Poll::Pending }
}
}
#[derive(Debug)]
pub struct Skip(pub usize);
impl Future for Skip {
type Output = ();
fn poll(mut self: Pin<&mut Self>, _: &mut Context) -> Poll<Self::Output> {
if self.0 == 0 { Poll::Ready(()) } else { self.0 -= 1; Poll::Pending }
}
}
#[derive(Debug)]
pub struct Pacer {
count: usize,
countdown: usize,
}
impl Pacer {
pub fn new(count: usize) -> Self { Pacer { count, countdown: count } }
pub fn count_update(&mut self, count: usize) -> &mut Self { self.count = count; self }
#[inline]
pub async fn step(&mut self, yield_count: usize, repeat: bool) {
if self.countdown > 0 { self.countdown -= 1 }
else {
if repeat { self.countdown = self.count }
if yield_count == 0 { Yield.await } else { Skip(yield_count).await }
}
}
#[inline(always)]
pub async fn throttle(&mut self) { self.step(0, false).await }
#[inline(always)]
pub async fn repeat(&mut self) { self.step(0, true).await }
#[inline(always)]
pub async fn burst(&mut self, i: usize) { self.step(i, true).await }
}