Skip to main content

cuckoo_runtime/
futures.rs

1//! Utility futures
2
3use std::{
4    pin::Pin,
5    task::{Context, Poll},
6};
7
8#[derive(Default, Debug)]
9struct Yield {
10    done: bool,
11}
12
13impl Future for Yield {
14    type Output = ();
15
16    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
17        println!("Polling {:?}", self);
18        if self.done {
19            return Poll::Ready(());
20        }
21
22        self.done = true;
23        cx.waker().wake_by_ref();
24
25        Poll::Pending
26    }
27}
28
29pub async fn yield_now() {
30    Yield::default().await
31}
32
33#[cfg(test)]
34mod tests {
35    use crate::Runtime;
36
37    use super::*;
38
39    #[test]
40    #[should_panic(expected = "got here")]
41    fn test_basic() {
42        let rt = Runtime::new(0);
43
44        rt.block_on(async move {
45            println!("hello");
46            yield_now().await;
47            panic!("got here");
48        });
49    }
50}