summaryrefslogtreecommitdiff
path: root/embassy-util/src/yield_now.rs
blob: 1ebecb916cdd7f3c3c0f63d697a3bf0631de96c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll};

/// Yield from the current task once, allowing other tasks to run.
pub fn yield_now() -> impl Future<Output = ()> {
    YieldNowFuture { yielded: false }
}

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
        }
    }
}