Skip to main content

psync/
future.rs

1use alloc::boxed::Box;
2use core::{future::Future, pin::Pin};
3
4pub type BoxFuture<'t, T = ()> = Pin<Box<dyn Future<Output = T> + Send + 't>>;
5pub type LocalBoxFuture<'t, T = ()> = Pin<Box<dyn Future<Output = T> + 't>>;
6
7/// Returns `Pending` once (after waking itself), letting the executor run other ready tasks.
8/// Works on any executor. `#[psync::graph(yield_every = N)]` inserts it into long loops.
9pub fn yield_now() -> YieldNow {
10    YieldNow(false)
11}
12
13/// The future returned by [`yield_now`].
14pub struct YieldNow(bool);
15
16impl Future for YieldNow {
17    type Output = ();
18    fn poll(mut self: Pin<&mut Self>, cx: &mut core::task::Context<'_>) -> core::task::Poll<()> {
19        if self.0 {
20            return core::task::Poll::Ready(());
21        }
22        self.0 = true;
23        cx.waker().wake_by_ref();
24        core::task::Poll::Pending
25    }
26}