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
use {sys, Evented, Token};
use event::{EventSet, IoEvent, PollOpt};
use std::{fmt, io};
pub struct Poll {
selector: sys::Selector,
events: sys::Events,
}
impl Poll {
pub fn new() -> io::Result<Poll> {
Ok(Poll {
selector: try!(sys::Selector::new()),
events: sys::Events::new(),
})
}
pub fn register<E: ?Sized>(&mut self, io: &E, token: Token, interest: EventSet, opts: PollOpt) -> io::Result<()>
where E: Evented
{
trace!("registering with poller");
try!(io.register(&mut self.selector, token, interest, opts));
Ok(())
}
pub fn reregister<E: ?Sized>(&mut self, io: &E, token: Token, interest: EventSet, opts: PollOpt) -> io::Result<()>
where E: Evented
{
trace!("registering with poller");
try!(io.reregister(&mut self.selector, token, interest, opts));
Ok(())
}
pub fn deregister<E: ?Sized>(&mut self, io: &E) -> io::Result<()>
where E: Evented
{
trace!("deregistering IO with poller");
try!(io.deregister(&mut self.selector));
Ok(())
}
pub fn poll(&mut self, timeout_ms: Option<usize>) -> io::Result<usize> {
try!(self.selector.select(&mut self.events, timeout_ms));
Ok(self.events.len())
}
pub fn event(&self, idx: usize) -> IoEvent {
self.events.get(idx)
}
pub fn events(&self) -> Events {
Events {
curr: 0,
poll: self,
}
}
}
impl fmt::Debug for Poll {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "Poll")
}
}
pub struct Events<'a> {
curr: usize,
poll: &'a Poll,
}
impl<'a> Iterator for Events<'a> {
type Item = IoEvent;
fn next(&mut self) -> Option<IoEvent> {
if self.curr == self.poll.events.len() {
return None;
}
let ret = self.poll.event(self.curr);
self.curr += 1;
Some(ret)
}
}