mio/
interest.rs

1use std::num::NonZeroU8;
2use 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)]
17pub struct Interest(NonZeroU8);
18
19// These must be unique.
20const READABLE: u8 = 0b0001;
21const WRITABLE: u8 = 0b0010;
22// The following are not available on all platforms.
23const AIO: u8 = 0b0100;
24const LIO: u8 = 0b1000;
25const PRIORITY: u8 = 0b10000;
26
27impl 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    #[must_use = "this returns the result of the operation, without modifying the original"]
67    pub const fn add(self, other: Interest) -> Interest {
68        Interest(unsafe { NonZeroU8::new_unchecked(self.0.get() | other.0.get()) })
69    }
70
71    /// Removes `other` `Interest` from `self`.
72    ///
73    /// Returns `None` if the set would be empty after removing `other`.
74    ///
75    /// ```
76    /// use mio::Interest;
77    ///
78    /// const RW_INTERESTS: Interest = Interest::READABLE.add(Interest::WRITABLE);
79    ///
80    /// // As long a one interest remain this will return `Some`.
81    /// let w_interest = RW_INTERESTS.remove(Interest::READABLE).unwrap();
82    /// assert!(!w_interest.is_readable());
83    /// assert!(w_interest.is_writable());
84    ///
85    /// // Removing all interests from the set will return `None`.
86    /// assert_eq!(w_interest.remove(Interest::WRITABLE), None);
87    ///
88    /// // Its also possible to remove multiple interests at once.
89    /// assert_eq!(RW_INTERESTS.remove(RW_INTERESTS), None);
90    /// ```
91    #[must_use = "this returns the result of the operation, without modifying the original"]
92    pub fn remove(self, other: Interest) -> Option<Interest> {
93        NonZeroU8::new(self.0.get() & !other.0.get()).map(Interest)
94    }
95
96    /// Returns true if the value includes readable readiness.
97    #[must_use]
98    pub const fn is_readable(self) -> bool {
99        (self.0.get() & READABLE) != 0
100    }
101
102    /// Returns true if the value includes writable readiness.
103    #[must_use]
104    pub const fn is_writable(self) -> bool {
105        (self.0.get() & WRITABLE) != 0
106    }
107
108    /// Returns true if `Interest` contains AIO readiness.
109    #[must_use]
110    pub const fn is_aio(self) -> bool {
111        (self.0.get() & AIO) != 0
112    }
113
114    /// Returns true if `Interest` contains LIO readiness.
115    #[must_use]
116    pub const fn is_lio(self) -> bool {
117        (self.0.get() & LIO) != 0
118    }
119
120    /// Returns true if `Interest` contains priority readiness.
121    #[must_use]
122    pub const fn is_priority(self) -> bool {
123        (self.0.get() & PRIORITY) != 0
124    }
125}
126
127impl ops::BitOr for Interest {
128    type Output = Self;
129
130    #[inline]
131    fn bitor(self, other: Self) -> Self {
132        self.add(other)
133    }
134}
135
136impl ops::BitOrAssign for Interest {
137    #[inline]
138    fn bitor_assign(&mut self, other: Self) {
139        self.0 = (*self | other).0;
140    }
141}
142
143impl fmt::Debug for Interest {
144    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
145        let mut one = false;
146        if self.is_readable() {
147            if one {
148                write!(fmt, " | ")?
149            }
150            write!(fmt, "READABLE")?;
151            one = true
152        }
153        if self.is_writable() {
154            if one {
155                write!(fmt, " | ")?
156            }
157            write!(fmt, "WRITABLE")?;
158            one = true
159        }
160        #[cfg(any(
161            target_os = "dragonfly",
162            target_os = "freebsd",
163            target_os = "ios",
164            target_os = "macos",
165            target_os = "tvos",
166            target_os = "watchos",
167        ))]
168        {
169            if self.is_aio() {
170                if one {
171                    write!(fmt, " | ")?
172                }
173                write!(fmt, "AIO")?;
174                one = true
175            }
176        }
177        #[cfg(target_os = "freebsd")]
178        {
179            if self.is_lio() {
180                if one {
181                    write!(fmt, " | ")?
182                }
183                write!(fmt, "LIO")?;
184                one = true
185            }
186        }
187        #[cfg(any(target_os = "linux", target_os = "android"))]
188        {
189            if self.is_priority() {
190                if one {
191                    write!(fmt, " | ")?
192                }
193                write!(fmt, "PRIORITY")?;
194                one = true
195            }
196        }
197        debug_assert!(one, "printing empty interests");
198        Ok(())
199    }
200}