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
use std::cell::RefCell;
use std::io;
use std::rc::Rc;
use mio::{Evented, Poll, PollOpt, Ready, Token};
use list::ErasedList;
pub mod channel;
pub mod generic;
#[cfg(target_os = "linux")]
pub mod signals;
pub mod timer;
pub trait EventSource: Evented {
type Event;
fn interest(&self) -> Ready;
fn pollopts(&self) -> PollOpt;
fn make_dispatcher<Data: 'static, F: FnMut(Self::Event, &mut Data) + 'static>(
&self,
callback: F,
) -> Rc<RefCell<EventDispatcher<Data>>>;
}
pub trait EventDispatcher<Data> {
fn ready(&mut self, ready: Ready, data: &mut Data);
}
pub struct Source<E: EventSource> {
pub(crate) source: E,
pub(crate) poll: Rc<Poll>,
pub(crate) list: Rc<RefCell<ErasedList>>,
pub(crate) token: Token,
}
impl<E: EventSource> Source<E> {
pub fn reregister(&self) -> io::Result<()> {
self.poll.reregister(
&self.source,
self.token,
self.source.interest(),
self.source.pollopts(),
)
}
pub fn remove(self) -> E {
let _ = self.poll.deregister(&self.source);
let _dispatcher = self.list.borrow_mut().del_source(self.token);
self.source
}
}
impl<E: EventSource> ::std::ops::Deref for Source<E> {
type Target = E;
fn deref(&self) -> &E {
&self.source
}
}
impl<E: EventSource> ::std::ops::DerefMut for Source<E> {
fn deref_mut(&mut self) -> &mut E {
&mut self.source
}
}
pub struct Idle {
pub(crate) callback: Rc<RefCell<ErasedIdle>>,
}
impl Idle {
pub fn cancel(self) {
self.callback.borrow_mut().cancel();
}
}
pub(crate) trait ErasedIdle {
fn cancel(&mut self);
}
impl<Data> ErasedIdle for Option<Box<FnMut(&mut Data)>> {
fn cancel(&mut self) {
self.take();
}
}