1 | use std::num::NonZeroU8; |
2 | use std::{fmt, ops}; |
3 | |
4 | /// Interest used in registering. |
5 | /// |
6 | /// Interest are used in [registering] [`event::Source`]s with [`Poll`], they |
7 | /// indicate what readiness should be monitored for. For example if a socket is |
8 | /// registered with [readable] interests and the socket becomes writable, no |
9 | /// event will be returned from a call to [`poll`]. |
10 | /// |
11 | /// [registering]: struct.Registry.html#method.register |
12 | /// [`event::Source`]: ./event/trait.Source.html |
13 | /// [`Poll`]: struct.Poll.html |
14 | /// [readable]: struct.Interest.html#associatedconstant.READABLE |
15 | /// [`poll`]: struct.Poll.html#method.poll |
16 | #[derive (Copy, PartialEq, Eq, Clone, PartialOrd, Ord)] |
17 | pub struct Interest(NonZeroU8); |
18 | |
19 | // These must be unique. |
20 | const READABLE: u8 = 0b0001; |
21 | const WRITABLE: u8 = 0b0010; |
22 | // The following are not available on all platforms. |
23 | const AIO: u8 = 0b0100; |
24 | const LIO: u8 = 0b1000; |
25 | const PRIORITY: u8 = 0b10000; |
26 | |
27 | impl Interest { |
28 | /// Returns a `Interest` set representing readable interests. |
29 | pub const READABLE: Interest = Interest(unsafe { NonZeroU8::new_unchecked(READABLE) }); |
30 | |
31 | /// Returns a `Interest` set representing writable interests. |
32 | pub const WRITABLE: Interest = Interest(unsafe { NonZeroU8::new_unchecked(WRITABLE) }); |
33 | |
34 | /// Returns a `Interest` set representing AIO completion interests. |
35 | #[cfg (any( |
36 | target_os = "dragonfly" , |
37 | target_os = "freebsd" , |
38 | target_os = "ios" , |
39 | target_os = "macos" , |
40 | target_os = "tvos" , |
41 | target_os = "watchos" , |
42 | ))] |
43 | pub const AIO: Interest = Interest(unsafe { NonZeroU8::new_unchecked(AIO) }); |
44 | |
45 | /// Returns a `Interest` set representing LIO completion interests. |
46 | #[cfg (target_os = "freebsd" )] |
47 | pub const LIO: Interest = Interest(unsafe { NonZeroU8::new_unchecked(LIO) }); |
48 | |
49 | /// Returns a `Interest` set representing priority completion interests. |
50 | #[cfg (any(target_os = "linux" , target_os = "android" ))] |
51 | pub const PRIORITY: Interest = Interest(unsafe { NonZeroU8::new_unchecked(PRIORITY) }); |
52 | |
53 | /// Add together two `Interest`. |
54 | /// |
55 | /// This does the same thing as the `BitOr` implementation, but is a |
56 | /// constant function. |
57 | /// |
58 | /// ``` |
59 | /// use mio::Interest; |
60 | /// |
61 | /// const INTERESTS: Interest = Interest::READABLE.add(Interest::WRITABLE); |
62 | /// # fn silent_dead_code_warning(_: Interest) { } |
63 | /// # silent_dead_code_warning(INTERESTS) |
64 | /// ``` |
65 | #[allow (clippy::should_implement_trait)] |
66 | pub const fn add(self, other: Interest) -> Interest { |
67 | Interest(unsafe { NonZeroU8::new_unchecked(self.0.get() | other.0.get()) }) |
68 | } |
69 | |
70 | /// Removes `other` `Interest` from `self`. |
71 | /// |
72 | /// Returns `None` if the set would be empty after removing `other`. |
73 | /// |
74 | /// ``` |
75 | /// use mio::Interest; |
76 | /// |
77 | /// const RW_INTERESTS: Interest = Interest::READABLE.add(Interest::WRITABLE); |
78 | /// |
79 | /// // As long a one interest remain this will return `Some`. |
80 | /// let w_interest = RW_INTERESTS.remove(Interest::READABLE).unwrap(); |
81 | /// assert!(!w_interest.is_readable()); |
82 | /// assert!(w_interest.is_writable()); |
83 | /// |
84 | /// // Removing all interests from the set will return `None`. |
85 | /// assert_eq!(w_interest.remove(Interest::WRITABLE), None); |
86 | /// |
87 | /// // Its also possible to remove multiple interests at once. |
88 | /// assert_eq!(RW_INTERESTS.remove(RW_INTERESTS), None); |
89 | /// ``` |
90 | pub fn remove(self, other: Interest) -> Option<Interest> { |
91 | NonZeroU8::new(self.0.get() & !other.0.get()).map(Interest) |
92 | } |
93 | |
94 | /// Returns true if the value includes readable readiness. |
95 | pub const fn is_readable(self) -> bool { |
96 | (self.0.get() & READABLE) != 0 |
97 | } |
98 | |
99 | /// Returns true if the value includes writable readiness. |
100 | pub const fn is_writable(self) -> bool { |
101 | (self.0.get() & WRITABLE) != 0 |
102 | } |
103 | |
104 | /// Returns true if `Interest` contains AIO readiness. |
105 | pub const fn is_aio(self) -> bool { |
106 | (self.0.get() & AIO) != 0 |
107 | } |
108 | |
109 | /// Returns true if `Interest` contains LIO readiness. |
110 | pub const fn is_lio(self) -> bool { |
111 | (self.0.get() & LIO) != 0 |
112 | } |
113 | |
114 | /// Returns true if `Interest` contains priority readiness. |
115 | pub const fn is_priority(self) -> bool { |
116 | (self.0.get() & PRIORITY) != 0 |
117 | } |
118 | } |
119 | |
120 | impl ops::BitOr for Interest { |
121 | type Output = Self; |
122 | |
123 | #[inline ] |
124 | fn bitor(self, other: Self) -> Self { |
125 | self.add(other) |
126 | } |
127 | } |
128 | |
129 | impl ops::BitOrAssign for Interest { |
130 | #[inline ] |
131 | fn bitor_assign(&mut self, other: Self) { |
132 | self.0 = (*self | other).0; |
133 | } |
134 | } |
135 | |
136 | impl fmt::Debug for Interest { |
137 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
138 | let mut one = false; |
139 | if self.is_readable() { |
140 | if one { |
141 | write!(fmt, " | " )? |
142 | } |
143 | write!(fmt, "READABLE" )?; |
144 | one = true |
145 | } |
146 | if self.is_writable() { |
147 | if one { |
148 | write!(fmt, " | " )? |
149 | } |
150 | write!(fmt, "WRITABLE" )?; |
151 | one = true |
152 | } |
153 | #[cfg (any( |
154 | target_os = "dragonfly" , |
155 | target_os = "freebsd" , |
156 | target_os = "ios" , |
157 | target_os = "macos" , |
158 | target_os = "tvos" , |
159 | target_os = "watchos" , |
160 | ))] |
161 | { |
162 | if self.is_aio() { |
163 | if one { |
164 | write!(fmt, " | " )? |
165 | } |
166 | write!(fmt, "AIO" )?; |
167 | one = true |
168 | } |
169 | } |
170 | #[cfg (any(target_os = "freebsd" ))] |
171 | { |
172 | if self.is_lio() { |
173 | if one { |
174 | write!(fmt, " | " )? |
175 | } |
176 | write!(fmt, "LIO" )?; |
177 | one = true |
178 | } |
179 | } |
180 | #[cfg (any(target_os = "linux" , target_os = "android" ))] |
181 | { |
182 | if self.is_priority() { |
183 | if one { |
184 | write!(fmt, " | " )? |
185 | } |
186 | write!(fmt, "PRIORITY" )?; |
187 | one = true |
188 | } |
189 | } |
190 | debug_assert!(one, "printing empty interests" ); |
191 | Ok(()) |
192 | } |
193 | } |
194 | |