use core::{future::poll_fn, task::Poll};
pub async fn yield_now() {
let mut has_yielded = false;
poll_fn(move |cx| {
if has_yielded {
Poll::Ready(())
} else {
cx.waker().wake_by_ref(); has_yielded = true;
Poll::Pending
}
})
.await
}
#[cfg(test)]
mod tests {
use super::*;
use pollster::FutureExt;
#[test]
pub fn test_yield_finishes() {
assert!(
async {
yield_now().await;
true
}
.block_on(),
"Yield should fininsh"
);
}
}