embassy_futures/yield_now.rs
1use core::future::Future;
2use core::pin::Pin;
3use core::task::{Context, Poll};
4
5/// Yield from the current task once, allowing other tasks to run.
6///
7/// This can be used to easily and quickly implement simple async primitives
8/// without using wakers. The following snippet will wait for a condition to
9/// hold, while still allowing other tasks to run concurrently (not monopolizing
10/// the executor thread).
11///
12/// ```rust
13/// # use embassy_futures::{block_on, yield_now};
14/// # async fn test_fn() {
15/// # let mut iter_count: u32 = 0;
16/// # let mut some_condition = || { iter_count += 1; iter_count > 10 };
17/// while !some_condition() {
18/// yield_now().await;
19/// }
20/// # }
21/// # block_on(test_fn());
22/// ```
23///
24/// The downside is this will spin in a busy loop, using 100% of the CPU, while
25/// using wakers correctly would allow the CPU to sleep while waiting.
26///
27/// The internal implementation is: on first poll the future wakes itself and
28/// returns `Poll::Pending`. On second poll, it returns `Poll::Ready`.
29pub fn yield_now() -> impl Future<Output = ()> {
30 YieldNowFuture { yielded: false }
31}
32
33#[must_use = "futures do nothing unless you `.await` or poll them"]
34struct YieldNowFuture {
35 yielded: bool,
36}
37
38impl Future for YieldNowFuture {
39 type Output = ();
40 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
41 if self.yielded {
42 Poll::Ready(())
43 } else {
44 self.yielded = true;
45 cx.waker().wake_by_ref();
46 Poll::Pending
47 }
48 }
49}