1 #![cfg_attr(not(feature = "net"), allow(dead_code, unreachable_pub))] 2 3 use crate::io::driver::Ready; 4 5 use std::fmt; 6 use 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)] 14 pub struct Interest(mio::Interest); 15 16 impl 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 true if the value includes readable interest. 48 /// 49 /// # Examples 50 /// 51 /// ``` 52 /// use tokio::io::Interest; 53 /// 54 /// assert!(Interest::READABLE.is_readable()); 55 /// assert!(!Interest::WRITABLE.is_readable()); 56 /// 57 /// let both = Interest::READABLE | Interest::WRITABLE; 58 /// assert!(both.is_readable()); 59 /// ``` is_readable(self) -> bool60 pub const fn is_readable(self) -> bool { 61 self.0.is_readable() 62 } 63 64 /// Returns true if the value includes writable interest. 65 /// 66 /// # Examples 67 /// 68 /// ``` 69 /// use tokio::io::Interest; 70 /// 71 /// assert!(!Interest::READABLE.is_writable()); 72 /// assert!(Interest::WRITABLE.is_writable()); 73 /// 74 /// let both = Interest::READABLE | Interest::WRITABLE; 75 /// assert!(both.is_writable()); 76 /// ``` is_writable(self) -> bool77 pub const fn is_writable(self) -> bool { 78 self.0.is_writable() 79 } 80 81 /// Add together two `Interest` values. 82 /// 83 /// This function works from a `const` context. 84 /// 85 /// # Examples 86 /// 87 /// ``` 88 /// use tokio::io::Interest; 89 /// 90 /// const BOTH: Interest = Interest::READABLE.add(Interest::WRITABLE); 91 /// 92 /// assert!(BOTH.is_readable()); 93 /// assert!(BOTH.is_writable()); add(self, other: Interest) -> Interest94 pub const fn add(self, other: Interest) -> Interest { 95 Interest(self.0.add(other.0)) 96 } 97 98 // This function must be crate-private to avoid exposing a `mio` dependency. to_mio(self) -> mio::Interest99 pub(crate) const fn to_mio(self) -> mio::Interest { 100 self.0 101 } 102 mask(self) -> Ready103 pub(super) fn mask(self) -> Ready { 104 match self { 105 Interest::READABLE => Ready::READABLE | Ready::READ_CLOSED, 106 Interest::WRITABLE => Ready::WRITABLE | Ready::WRITE_CLOSED, 107 _ => Ready::EMPTY, 108 } 109 } 110 } 111 112 impl ops::BitOr for Interest { 113 type Output = Self; 114 115 #[inline] bitor(self, other: Self) -> Self116 fn bitor(self, other: Self) -> Self { 117 self.add(other) 118 } 119 } 120 121 impl ops::BitOrAssign for Interest { 122 #[inline] bitor_assign(&mut self, other: Self)123 fn bitor_assign(&mut self, other: Self) { 124 self.0 = (*self | other).0; 125 } 126 } 127 128 impl fmt::Debug for Interest { fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result129 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 130 self.0.fmt(fmt) 131 } 132 } 133