use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll};
#[derive(Debug)]
pub struct YieldNow {
yielded: bool,
}
impl YieldNow {
#[inline]
pub const fn new() -> Self {
Self { yielded: false }
}
}
impl Default for YieldNow {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl Future for YieldNow {
type Output = ();
#[inline]
fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.yielded {
Poll::Ready(())
} else {
self.yielded = true;
Poll::Pending
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::async_vm::block_on;
#[test]
fn test_yield_now() {
let mut call_count = 0;
block_on(async {
call_count += 1;
YieldNow::new().await;
call_count += 1;
});
assert_eq!(call_count, 2);
}
#[test]
fn test_multiple_yields() {
let mut yields = 0;
block_on(async {
for _ in 0..10 {
YieldNow::new().await;
yields += 1;
}
});
assert_eq!(yields, 10);
}
}