async_core/
yielding.rs

1use core::{
2    future::Future,
3    pin::Pin,
4    task::{Context, Poll},
5};
6
7/// A future which returns Pending once, and returns Ready the next time it is polled. Used to
8/// interrupt a running task to make space for others to run.
9pub struct YieldFuture(bool);
10
11impl Future for YieldFuture {
12    type Output = ();
13
14    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
15        if self.0 {
16            Poll::Ready(())
17        } else {
18            self.get_mut().0 = true;
19            Poll::Pending
20        }
21    }
22}
23
24/// Interrupts the current task and yields for the ohers. This uses a YieldFuture
25pub fn yield_now() -> YieldFuture {
26    YieldFuture(false)
27}