embassy/embassy-futures/src/yield_now.rs

44 lines
1.3 KiB
Rust
Raw Normal View History

2022-01-12 16:30:37 +01:00
use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll};
/// Yield from the current task once, allowing other tasks to run.
2022-08-29 00:54:14 +02:00
///
/// This can be used to easily and quickly implement simple async primitives
/// without using wakers. The following snippet will wait for a condition to
/// hold, while still allowing other tasks to run concurrently (not monopolizing
/// the executor thread).
///
/// ```rust,no_run
/// while !some_condition() {
/// yield_now().await;
/// }
/// ```
///
/// The downside is this will spin in a busy loop, using 100% of the CPU, while
/// using wakers correctly would allow the CPU to sleep while waiting.
///
/// The internal implementation is: on first poll the future wakes itself and
/// returns `Poll::Pending`. On second poll, it returns `Poll::Ready`.
2022-01-12 16:30:37 +01:00
pub fn yield_now() -> impl Future<Output = ()> {
YieldNowFuture { yielded: false }
}
2023-02-24 20:01:41 +01:00
#[must_use = "futures do nothing unless you `.await` or poll them"]
2022-01-12 16:30:37 +01:00
struct YieldNowFuture {
yielded: bool,
}
impl Future for YieldNowFuture {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.yielded {
Poll::Ready(())
} else {
self.yielded = true;
cx.waker().wake_by_ref();
Poll::Pending
}
}
}