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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#![cfg_attr(not(feature = "net"), allow(unreachable_pub))]
use std::fmt;
use std::ops;
const READABLE: usize = 0b0_01;
const WRITABLE: usize = 0b0_10;
const READ_CLOSED: usize = 0b0_0100;
const WRITE_CLOSED: usize = 0b0_1000;
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Ready(usize);
impl Ready {
pub const EMPTY: Ready = Ready(0);
pub const READABLE: Ready = Ready(READABLE);
pub const WRITABLE: Ready = Ready(WRITABLE);
pub const READ_CLOSED: Ready = Ready(READ_CLOSED);
pub const WRITE_CLOSED: Ready = Ready(WRITE_CLOSED);
pub const ALL: Ready = Ready(READABLE | WRITABLE | READ_CLOSED | WRITE_CLOSED);
pub(crate) fn from_mio(event: &mio::event::Event) -> Ready {
let mut ready = Ready::EMPTY;
#[cfg(all(target_os = "freebsd", feature = "net"))]
{
if event.is_aio() {
ready |= Ready::READABLE;
}
if event.is_lio() {
ready |= Ready::READABLE;
}
}
if event.is_readable() {
ready |= Ready::READABLE;
}
if event.is_writable() {
ready |= Ready::WRITABLE;
}
if event.is_read_closed() {
ready |= Ready::READ_CLOSED;
}
if event.is_write_closed() {
ready |= Ready::WRITE_CLOSED;
}
ready
}
pub fn is_empty(self) -> bool {
self == Ready::EMPTY
}
pub fn is_readable(self) -> bool {
self.contains(Ready::READABLE) || self.is_read_closed()
}
pub fn is_writable(self) -> bool {
self.contains(Ready::WRITABLE) || self.is_write_closed()
}
pub fn is_read_closed(self) -> bool {
self.contains(Ready::READ_CLOSED)
}
pub fn is_write_closed(self) -> bool {
self.contains(Ready::WRITE_CLOSED)
}
pub(crate) fn contains<T: Into<Self>>(self, other: T) -> bool {
let other = other.into();
(self & other) == other
}
pub(crate) fn from_usize(val: usize) -> Ready {
Ready(val & Ready::ALL.as_usize())
}
pub(crate) fn as_usize(self) -> usize {
self.0
}
}
cfg_io_readiness! {
use crate::io::Interest;
impl Ready {
pub(crate) fn from_interest(interest: Interest) -> Ready {
let mut ready = Ready::EMPTY;
if interest.is_readable() {
ready |= Ready::READABLE;
ready |= Ready::READ_CLOSED;
}
if interest.is_writable() {
ready |= Ready::WRITABLE;
ready |= Ready::WRITE_CLOSED;
}
ready
}
pub(crate) fn intersection(self, interest: Interest) -> Ready {
Ready(self.0 & Ready::from_interest(interest).0)
}
pub(crate) fn satisfies(self, interest: Interest) -> bool {
self.0 & Ready::from_interest(interest).0 != 0
}
}
}
impl ops::BitOr<Ready> for Ready {
type Output = Ready;
#[inline]
fn bitor(self, other: Ready) -> Ready {
Ready(self.0 | other.0)
}
}
impl ops::BitOrAssign<Ready> for Ready {
#[inline]
fn bitor_assign(&mut self, other: Ready) {
self.0 |= other.0;
}
}
impl ops::BitAnd<Ready> for Ready {
type Output = Ready;
#[inline]
fn bitand(self, other: Ready) -> Ready {
Ready(self.0 & other.0)
}
}
impl ops::Sub<Ready> for Ready {
type Output = Ready;
#[inline]
fn sub(self, other: Ready) -> Ready {
Ready(self.0 & !other.0)
}
}
impl fmt::Debug for Ready {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("Ready")
.field("is_readable", &self.is_readable())
.field("is_writable", &self.is_writable())
.field("is_read_closed", &self.is_read_closed())
.field("is_write_closed", &self.is_write_closed())
.finish()
}
}