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
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Event {
None,
Read,
Write,
Error,
EdgeTriggered,
HangUp,
OneShot,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Events(u32);
impl std::fmt::Display for Events {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "0x{:08X}", self.0)
}
}
impl Events {
pub fn new() -> Self {
Self { 0: 0 }
}
pub fn none(mut self) -> Self {
self.0 = 0;
self
}
pub fn read(mut self) -> Self {
self.0 |= 1 << Event::Read as u32;
self
}
pub fn write(mut self) -> Self {
self.0 |= 1 << Event::Write as u32;
self
}
pub fn error(mut self) -> Self {
self.0 |= 1 << Event::Error as u32;
self
}
pub fn is_none(self) -> bool {
self.0 == 0
}
pub fn has_read(self) -> bool {
(self.0 & (1 << Event::Read as u32)) != 0
}
pub fn has_write(self) -> bool {
(self.0 & (1 << Event::Write as u32)) != 0
}
pub fn has_error(self) -> bool {
(self.0 & (1 << Event::Error as u32)) != 0
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SysError(i32);
impl std::fmt::Display for SysError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, r#"Code={}, Reason="{{}}")"#, self.0)
}
}
impl std::error::Error for SysError {}
impl From<i32> for SysError {
fn from(val: i32) -> Self {
Self { 0: val }
}
}
impl Into<i32> for SysError {
fn into(self) -> i32 {
self.0
}
}
impl SysError {
pub fn last() -> Self {
unsafe {
Self {
0: *(libc::__errno_location()),
}
}
}
}
#[cfg(target_os = "linux")]
pub mod epoll;
#[cfg(target_os = "linux")]
#[doc(inline)]
pub use epoll::{EventContext, EventData, Poller};
#[cfg(not(target_os = "linux"))]
pub mod select;
#[cfg(test)]
mod tests {}