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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! Futures to interrupt the current async context to give timeslices to other pending tasks

use core::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

/// A future that can be polled once before it becomes ready; useful to cooperatively give up a timeslice to the
/// runtime/other pending futures
///
/// # Behaviour
/// Polling this future will immediately wake the waker again and yield, making room for other futures to execute. This
/// is useful for e.g. running intensive loops or similar inside a future.
#[derive(Debug, Default)]
pub struct SpinFuture {
    /// Whether the future has been polled already or not
    polled: bool,
}
impl SpinFuture {
    /// Creates a new spin future
    ///
    /// # Note
    /// This future should usually not be constructed directly, use [`spin_once`] instead.
    pub const fn new() -> Self {
        Self { polled: false }
    }
}
impl Future for SpinFuture {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        if !self.polled {
            // Mark the future as polled so that it returns ready the next time
            self.polled = true;
            cx.waker().wake_by_ref();
            Poll::Pending
        } else {
            // The future has been polled, so it's ready now
            Poll::Ready(())
        }
    }
}

/// A passive future that can be polled once before it becomes ready; useful to suspend the current context and up a
/// timeslice to the runtime/other pending futures
///
/// # Behaviour
/// Polling this future yields immediately **WITHOUT WAKING THE WAKER AGAIN**, making room for other futures to execute.
/// This is useful to e.g. suspend the execution of the current routine if it cannot make any progress and has no
/// associated event sources.
///
/// # Important
/// Unlike [`SpinFuture`], this future **DOES NOT** wake the waker again. If the runtime enters deep-sleep, you **MUST**
/// ensure there are other pending futures or event sources to wake it up again.
#[derive(Debug, Default)]
pub struct SleepFuture {
    /// Whether the future has been polled already or not
    polled: bool,
}
impl SleepFuture {
    /// Creates a new spin future
    ///
    /// # Note
    /// This future should usually not be constructed directly, use [`sleep_once`] instead.
    pub const fn new() -> Self {
        Self { polled: false }
    }
}
impl Future for SleepFuture {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
        if !self.polled {
            // Mark the future as polled so that it returns ready the next time
            self.polled = true;
            Poll::Pending
        } else {
            // The future has been polled, so it's ready now
            Poll::Ready(())
        }
    }
}

/// A function that can be awaited once before it returns; useful to cooperatively give up a timeslice to the
/// runtime/other pending futures
///
/// # Behaviour
/// Awaiting this function will immediately wake the waker again and yield, making room for other futures to execute.
/// This is useful for e.g. running intensive loops or similar inside a future.
pub async fn spin_once() {
    SpinFuture::new().await
}

/// A function that can be polled once before it becomes ready; useful to suspend the current context and up a timeslice
/// to the runtime/other pending futures
///
/// # Behaviour
/// Awaiting this function yields immediately **WITHOUT WAKING THE WAKER AGAIN**, making room for other futures to
/// execute. This is useful to e.g. suspend the execution of the current routine if it cannot make any progress and has
/// no associated event sources.
///
/// # Important
/// Unlike [`spin_once`], this function **DOES NOT** wake the waker again. If the runtime enters deep-sleep, you
/// **MUST** ensure there are other pending futures or event sources to wake it up again.
pub async fn sleep_once() {
    SleepFuture::new().await
}