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
use super::{Revision, Runtime};
use futures::{
    stream::{Stream, StreamExt},
    task::LocalSpawn,
};
use std::{
    pin::Pin,
    task::{Context as FutContext, Poll, Waker},
};
pub struct RunLoop<Root> {
    inner: Runtime,
    root: Root,
}
impl super::Runtime {
    
    
    pub fn looped<Root, Out>(self, root: Root) -> RunLoop<Root>
    where
        Root: FnMut() -> Out,
    {
        RunLoop { inner: self, root }
    }
}
impl<Root, Out> RunLoop<Root>
where
    Root: FnMut() -> Out + Unpin,
{
    
    pub fn new(root: Root) -> RunLoop<Root> {
        RunLoop { root, inner: Runtime::new() }
    }
    
    pub fn revision(&self) -> Revision {
        self.inner.revision()
    }
    
    
    pub fn set_state_change_waker(&mut self, wk: Waker) {
        self.inner.set_state_change_waker(wk);
    }
    
    pub fn set_task_executor(&mut self, sp: impl LocalSpawn + 'static) {
        self.inner.set_task_executor(sp);
    }
    
    
    pub fn run_once(&mut self) -> Out {
        self.inner.run_once(&mut self.root)
    }
    
    
    
    pub async fn run_on_state_changes(mut self) {
        loop {
            self.next().await;
        }
    }
    
    pub fn unloop(self) -> (Runtime, Root) {
        (self.inner, self.root)
    }
}
impl<Root, Out> Stream for RunLoop<Root>
where
    Root: FnMut() -> Out + Unpin,
{
    type Item = (Revision, Out);
    
    
    fn poll_next(self: Pin<&mut Self>, cx: &mut FutContext<'_>) -> Poll<Option<Self::Item>> {
        let this = self.get_mut();
        this.inner.set_state_change_waker(cx.waker().clone());
        let out = this.run_once();
        Poll::Ready(Some((this.inner.revision, out)))
    }
}