1#![cfg_attr(not(feature = "net"), allow(dead_code, unreachable_pub))]
2
3use crate::io::ready::Ready;
4
5use std::fmt;
6use std::ops;
7
8/// Readiness event interest.
9///
10/// Specifies the readiness events the caller is interested in when awaiting on
11/// I/O resource readiness states.
12#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
13#[derive(Clone, Copy, Eq, PartialEq)]
14pub struct Interest(mio::Interest);
15
16impl Interest {
17 // The non-FreeBSD definitions in this block are active only when
18 // building documentation.
19 cfg_aio! {
20 /// Interest for POSIX AIO.
21 #[cfg(target_os = "freebsd")]
22 pub const AIO: Interest = Interest(mio::Interest::AIO);
23
24 /// Interest for POSIX AIO.
25 #[cfg(not(target_os = "freebsd"))]
26 pub const AIO: Interest = Interest(mio::Interest::READABLE);
27
28 /// Interest for POSIX AIO lio_listio events.
29 #[cfg(target_os = "freebsd")]
30 pub const LIO: Interest = Interest(mio::Interest::LIO);
31
32 /// Interest for POSIX AIO lio_listio events.
33 #[cfg(not(target_os = "freebsd"))]
34 pub const LIO: Interest = Interest(mio::Interest::READABLE);
35 }
36
37 /// Interest in all readable events.
38 ///
39 /// Readable interest includes read-closed events.
40 pub const READABLE: Interest = Interest(mio::Interest::READABLE);
41
42 /// Interest in all writable events.
43 ///
44 /// Writable interest includes write-closed events.
45 pub const WRITABLE: Interest = Interest(mio::Interest::WRITABLE);
46
47 /// Returns a `Interest` set representing priority completion interests.
48 #[cfg(any(target_os = "linux", target_os = "android"))]
49 #[cfg_attr(docsrs, doc(cfg(any(target_os = "linux", target_os = "android"))))]
50 pub const PRIORITY: Interest = Interest(mio::Interest::PRIORITY);
51
52 /// Returns true if the value includes readable interest.
53 ///
54 /// # Examples
55 ///
56 /// ```
57 /// use tokio::io::Interest;
58 ///
59 /// assert!(Interest::READABLE.is_readable());
60 /// assert!(!Interest::WRITABLE.is_readable());
61 ///
62 /// let both = Interest::READABLE | Interest::WRITABLE;
63 /// assert!(both.is_readable());
64 /// ```
65 pub const fn is_readable(self) -> bool {
66 self.0.is_readable()
67 }
68
69 /// Returns true if the value includes writable interest.
70 ///
71 /// # Examples
72 ///
73 /// ```
74 /// use tokio::io::Interest;
75 ///
76 /// assert!(!Interest::READABLE.is_writable());
77 /// assert!(Interest::WRITABLE.is_writable());
78 ///
79 /// let both = Interest::READABLE | Interest::WRITABLE;
80 /// assert!(both.is_writable());
81 /// ```
82 pub const fn is_writable(self) -> bool {
83 self.0.is_writable()
84 }
85
86 /// Returns true if the value includes priority interest.
87 ///
88 /// # Examples
89 ///
90 /// ```
91 /// use tokio::io::Interest;
92 ///
93 /// assert!(!Interest::READABLE.is_priority());
94 /// assert!(Interest::PRIORITY.is_priority());
95 ///
96 /// let both = Interest::READABLE | Interest::PRIORITY;
97 /// assert!(both.is_priority());
98 /// ```
99 #[cfg(any(target_os = "linux", target_os = "android"))]
100 #[cfg_attr(docsrs, doc(cfg(any(target_os = "linux", target_os = "android"))))]
101 pub const fn is_priority(self) -> bool {
102 self.0.is_priority()
103 }
104
105 /// Add together two `Interest` values.
106 ///
107 /// This function works from a `const` context.
108 ///
109 /// # Examples
110 ///
111 /// ```
112 /// use tokio::io::Interest;
113 ///
114 /// const BOTH: Interest = Interest::READABLE.add(Interest::WRITABLE);
115 ///
116 /// assert!(BOTH.is_readable());
117 /// assert!(BOTH.is_writable());
118 pub const fn add(self, other: Interest) -> Interest {
119 Interest(self.0.add(other.0))
120 }
121
122 // This function must be crate-private to avoid exposing a `mio` dependency.
123 pub(crate) const fn to_mio(self) -> mio::Interest {
124 self.0
125 }
126
127 pub(crate) fn mask(self) -> Ready {
128 match self {
129 Interest::READABLE => Ready::READABLE | Ready::READ_CLOSED,
130 Interest::WRITABLE => Ready::WRITABLE | Ready::WRITE_CLOSED,
131 #[cfg(any(target_os = "linux", target_os = "android"))]
132 Interest::PRIORITY => Ready::PRIORITY | Ready::READ_CLOSED,
133 _ => Ready::EMPTY,
134 }
135 }
136}
137
138impl ops::BitOr for Interest {
139 type Output = Self;
140
141 #[inline]
142 fn bitor(self, other: Self) -> Self {
143 self.add(other)
144 }
145}
146
147impl ops::BitOrAssign for Interest {
148 #[inline]
149 fn bitor_assign(&mut self, other: Self) {
150 self.0 = (*self | other).0;
151 }
152}
153
154impl fmt::Debug for Interest {
155 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
156 self.0.fmt(fmt)
157 }
158}
159