-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01b.rs
78 lines (61 loc) · 1.66 KB
/
01b.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
#![allow(unused)]
type Rt<T = ()> = Result<T, Box<dyn std::error::Error>>;
#[derive(Debug)]
pub struct Receiver<T>(std::marker::PhantomData<T>);
#[derive(Debug)]
pub struct Sender<T>(std::marker::PhantomData<T>);
impl<T> Sender<T> {
fn send(&self, _: T) -> Rt {
Ok(())
}
}
#[derive(Debug)]
pub struct Event;
#[derive(Debug)]
pub enum Action {
Foo,
}
fn unbounded_channel<T>() -> (Sender<T>, Receiver<T>) {
(Sender(std::marker::PhantomData), Receiver(std::marker::PhantomData))
}
rcstruct::rcstruct! {
pub struct GUI {
running: bool,
event_recv: Receiver<Event>,
action_send: Sender<Action>,
}
impl {
pub new(event_recv: Receiver<Event>, action_send: Sender<Action>) -> Rt<Self> {
let running = true;
{ running, event_recv, action_send, }
}
pub fn send_action(&self, action: Action) -> Rt {
Ok(self.action_send.send(action)?)
}
pub fn running(&self) -> Rt<bool> {
Ok(self.running)
}
pub fn quit(&mut self) -> Rt {
self.running = false;
Ok(())
}
pub fn events(&self) -> Rt<impl IntoIterator<Item = Event>> {
let events = Vec::new();
Ok(events)
}
}
}
fn main() -> Rt {
// Set up channels.
let (event_send, event_recv) = unbounded_channel::<Event>();
let (action_send, action_recv) = unbounded_channel::<Action>();
let gui = GUI::new(event_recv, action_send)?;
while gui.running()? {
for ev in gui.events()? {
// ...
}
gui.send_action(Action::Foo)?;
gui.quit()?;
}
Ok(())
}