-
Notifications
You must be signed in to change notification settings - Fork 4
/
bar.rs
216 lines (180 loc) · 3.78 KB
/
bar.rs
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::ops;
/*
yield fn state_machine() -> yield usize {
println!("before");
yield 1;
yield 2;
println!("after");
}
*/
struct StateMachine {
state: usize,
}
impl StateMachine {
fn next(&mut self) -> Option<usize> {
loop {
match self.state {
0 => {
println!("before");
self.state = 1;
}
1 => {
self.state = 2;
return Some(1);
}
2 => {
self.state = 3;
return Some(2);
}
_ => {
println!("after");
return None;
}
}
}
}
}
/*
async fn get_string_async() {
let mut is_loading: bool = true;
let result = await get_string();
is_loading = false;
}
fn get_string() -> Task<String> {
await sleep(2000);
String::new()
*/
enum AsyncResult<T, E> {
Ok(T),
Err(E),
Running,
}
struct Immutable<T> {
value: T,
}
impl<T> Immutable<T> {
fn new(value: T) -> Self {
Immutable {
value: value,
}
}
fn unwrap(self) -> T {
self.value
}
}
impl<T> ops::Deref for Immutable<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}
struct Mutable<T> {
value: T,
}
impl<T> Mutable<T> {
fn new(value: T) -> Self {
Mutable {
value: value,
}
}
fn set(&mut self, value: T) {
self.value = value;
}
fn unwrap(self) -> T {
self.value
}
}
impl<'a, T> ops::Deref for Mutable<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<'a, T> ops::DerefMut for Mutable<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.value
}
}
struct GetStringAsync {
state: usize,
data: GetStringAsyncData,
call: Option<GetString>,
}
struct GetStringAsyncData {
is_loading: Mutable<bool>,
}
impl GetStringAsync {
fn new() -> Self {
let is_loading = Mutable::new(true);
GetStringAsync {
state: 0,
data: GetStringAsyncData {
is_loading: is_loading,
}
}
}
fn next(&mut self) {
match self.state {
0 => {
}
}
}
}
struct GetString {
state: GetStringState,
async_sleep: Option<Sleep>,
data: GetStringData,
}
enum GetStringState {
State0(Sleep),
State1,
}
struct GetStringData;
impl GetString {
fn new() -> Self {
let async_sleep = Sleep::new(2000);
GetString {
state: GetStringState::State0(async_sleep),
data: GetStringData,
}
}
fn next(&mut self) -> AsyncResult<()> {
match self.state {
GetStringState::State0(ref mut async) => {
match async.next() {
AsyncResult::Ok(()) => {
self.state = GetStringState::State0;
}
AsyncResult::Running => {
return AsyncResult::Running;
}
AsyncResult::Err(err) => {
return AsyncResult::Err(err);
}
}
}
GetStringState::State1 => {
AsyncResult::Ok(())
}
}
}
}
struct Sleep {
end: usize,
}
impl Sleep {
fn new(time: usize) -> Self {
Sleep {
end: time::now() + time,
}
}
fn next(&mut self) -> AsyncResult<(), Void> {
if time::now() <= self.end {
AsyncResult::Ok(())
} else {
AsyncResult::Running
}
}
}
fn main() {
}