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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use {
crossbeam::channel::{self, bounded, select, Receiver},
std::thread,
termimad::TimedEvent,
};
pub enum Either<A, B> {
First(A),
Second(B),
}
#[derive(Debug, Clone)]
pub enum ComputationResult<V> {
NotComputed, Done(V),
None, }
impl<V> ComputationResult<V> {
pub fn is_done(&self) -> bool {
matches!(&self, Self::Done(_))
}
pub fn is_not_computed(&self) -> bool {
matches!(&self, Self::NotComputed)
}
pub fn is_some(&self) -> bool {
!matches!(&self, Self::None)
}
pub fn is_none(&self) -> bool {
matches!(&self, Self::None)
}
}
pub struct Dam {
receiver: Receiver<TimedEvent>,
in_dam: Option<TimedEvent>,
}
impl Dam {
pub fn from(receiver: Receiver<TimedEvent>) -> Self {
Self {
receiver,
in_dam: None,
}
}
pub fn unlimited() -> Self {
Self::from(channel::never())
}
pub fn observer(&self) -> DamObserver {
DamObserver::from(self)
}
pub fn try_compute<V: Send + 'static, F: Send + 'static + FnOnce() -> ComputationResult<V>>(
&mut self,
f: F,
) -> ComputationResult<V> {
let (comp_sender, comp_receiver) = bounded(1);
thread::spawn(move || {
let comp_res = time!("comp in dam", f());
if comp_sender.send(comp_res).is_err() {
debug!("no channel at end of computation");
}
});
self.select(comp_receiver)
}
pub fn select<V>(
&mut self,
comp_receiver: Receiver<ComputationResult<V>>,
) -> ComputationResult<V> {
if self.in_dam.is_some() {
debug!("There's already an event in dam");
ComputationResult::None
} else {
select! {
recv(self.receiver) -> event => {
debug!("dam interrupts computation");
self.in_dam = event.ok();
ComputationResult::None
}
recv(comp_receiver) -> comp_res => {
comp_res.unwrap_or(ComputationResult::None)
}
}
}
}
pub fn has_event(&self) -> bool {
!self.receiver.is_empty()
}
pub fn next_event(&mut self) -> Option<TimedEvent> {
if self.in_dam.is_some() {
self.in_dam.take()
} else {
match self.receiver.recv() {
Ok(event) => Some(event),
Err(_) => {
debug!("dead dam"); None
}
}
}
}
pub fn next<T>(&mut self, other: &Receiver<T>) -> Either<Option<TimedEvent>, Option<T>> {
if self.in_dam.is_some() {
Either::First(self.in_dam.take())
} else {
select! {
recv(self.receiver) -> event => Either::First(match event {
Ok(event) => Some(event),
Err(_) => {
debug!("dead dam"); None
}
}),
recv(other) -> o => Either::Second(match o {
Ok(o) => Some(o),
Err(_) => {
debug!("dead other");
None
}
}),
}
}
}
}
pub struct DamObserver {
receiver: Receiver<TimedEvent>,
}
impl DamObserver {
pub fn from(dam: &Dam) -> Self {
Self {
receiver: dam.receiver.clone(),
}
}
pub fn has_event(&self) -> bool {
!self.receiver.is_empty()
}
}
#[derive(Debug, Clone)]
pub enum Computation<V> {
InProgress(Receiver<ComputationResult<V>>),
Finished(ComputationResult<V>),
}