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 /// Interest in all readable events. 18 /// 19 /// Readable interest includes read-closed events. 20 pub const READABLE: Interest = Interest(mio::Interest::READABLE); 21 22 /// Interest in all writable events 23 /// 24 /// Writable interest includes write-closed events. 25 pub const WRITABLE: Interest = Interest(mio::Interest::WRITABLE); 26 27 /// Returns true if the value includes readable interest. 28 /// 29 /// # Examples 30 /// 31 /// ``` 32 /// use tokio::io::Interest; 33 /// 34 /// assert!(Interest::READABLE.is_readable()); 35 /// assert!(!Interest::WRITABLE.is_readable()); 36 /// 37 /// let both = Interest::READABLE | Interest::WRITABLE; 38 /// assert!(both.is_readable()); 39 /// ``` is_readable(self) -> bool40 pub const fn is_readable(self) -> bool { 41 self.0.is_readable() 42 } 43 44 /// Returns true if the value includes writable interest. 45 /// 46 /// # Examples 47 /// 48 /// ``` 49 /// use tokio::io::Interest; 50 /// 51 /// assert!(!Interest::READABLE.is_writable()); 52 /// assert!(Interest::WRITABLE.is_writable()); 53 /// 54 /// let both = Interest::READABLE | Interest::WRITABLE; 55 /// assert!(both.is_writable()); 56 /// ``` is_writable(self) -> bool57 pub const fn is_writable(self) -> bool { 58 self.0.is_writable() 59 } 60 61 /// Add together two `Interst` values. 62 /// 63 /// This function works from a `const` context. 64 /// 65 /// # Examples 66 /// 67 /// ``` 68 /// use tokio::io::Interest; 69 /// 70 /// const BOTH: Interest = Interest::READABLE.add(Interest::WRITABLE); 71 /// 72 /// assert!(BOTH.is_readable()); 73 /// assert!(BOTH.is_writable()); add(self, other: Interest) -> Interest74 pub const fn add(self, other: Interest) -> Interest { 75 Interest(self.0.add(other.0)) 76 } 77 78 // This function must be crate-private to avoid exposing a `mio` dependency. to_mio(self) -> mio::Interest79 pub(crate) const fn to_mio(self) -> mio::Interest { 80 self.0 81 } 82 mask(self) -> Ready83 pub(super) fn mask(self) -> Ready { 84 match self { 85 Interest::READABLE => Ready::READABLE | Ready::READ_CLOSED, 86 Interest::WRITABLE => Ready::WRITABLE | Ready::WRITE_CLOSED, 87 _ => Ready::EMPTY, 88 } 89 } 90 } 91 92 impl ops::BitOr for Interest { 93 type Output = Self; 94 95 #[inline] bitor(self, other: Self) -> Self96 fn bitor(self, other: Self) -> Self { 97 self.add(other) 98 } 99 } 100 101 impl ops::BitOrAssign for Interest { 102 #[inline] bitor_assign(&mut self, other: Self)103 fn bitor_assign(&mut self, other: Self) { 104 self.0 = (*self | other).0; 105 } 106 } 107 108 impl fmt::Debug for Interest { fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result109 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 110 self.0.fmt(fmt) 111 } 112 } 113