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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#![doc = include_str!("../README.md")]
#[doc(hidden)]
#[path = "exports.rs"]
pub mod __private;
pub mod context;
use context::StateContext;
use futures_core::Stream;
use std::{
ops::{Deref, DerefMut},
pin::Pin,
task::Poll,
};
pub trait AsyncComponent {
fn update_component(&mut self);
}
pub trait State {
type Output;
fn update(this: &mut Self) -> Option<Self::Output>;
}
#[derive(Debug)]
pub struct StateCell<T> {
cx: StateContext,
changed: bool,
inner: T,
}
impl<T> StateCell<T> {
pub fn new(cx: StateContext, inner: T) -> Self {
cx.signal();
Self {
cx,
changed: true,
inner,
}
}
pub fn invalidate(this: &mut Self) {
if !this.changed {
this.changed = true;
}
this.cx.signal()
}
}
impl<T> Deref for StateCell<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<T> DerefMut for StateCell<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
StateCell::invalidate(self);
&mut self.inner
}
}
impl<T> State for StateCell<T> {
type Output = ();
fn update(this: &mut Self) -> Option<Self::Output> {
if this.changed {
this.changed = false;
Some(())
} else {
None
}
}
}
impl<T> Drop for StateCell<T> {
fn drop(&mut self) {
self.cx.signal();
}
}
#[derive(Debug)]
pub struct StreamCell<T> {
cx: StateContext,
inner: T,
}
impl<T: Stream> StreamCell<T> {
pub fn new(cx: StateContext, inner: T) -> Self {
cx.signal();
Self { cx, inner }
}
}
impl<T: Stream + Unpin> State for StreamCell<T> {
type Output = T::Item;
fn update(this: &mut Self) -> Option<Self::Output> {
match Pin::new(&mut this.inner).poll_next(&mut this.cx.task_context()) {
Poll::Ready(Some(output)) => Some(output),
_ => None,
}
}
}
impl<T> Drop for StreamCell<T> {
fn drop(&mut self) {
self.cx.signal();
}
}