From 3317c758b0849ea3ac5819c38b24a9860931caed Mon Sep 17 00:00:00 2001 From: Marek Kuskowski <50183564+nylonicious@users.noreply.github.com> Date: Thu, 3 Aug 2023 06:27:26 +0200 Subject: [PATCH] io: add Interest::remove method --- tokio/src/io/interest.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tokio/src/io/interest.rs b/tokio/src/io/interest.rs index 9256bd238da..46d47a61383 100644 --- a/tokio/src/io/interest.rs +++ b/tokio/src/io/interest.rs @@ -167,6 +167,37 @@ impl Interest { Self(self.0 | other.0) } + /// Remove `Interest` from `self`. + /// + /// Returns `None` if the set would be empty after removing `Interest`. + /// + /// # Examples + /// + /// ``` + /// use tokio::io::Interest; + /// + /// const RW_INTEREST: Interest = Interest::READABLE.add(Interest::WRITABLE); + /// + /// let w_interest = RW_INTEREST.remove(Interest::READABLE).unwrap(); + /// assert!(!w_interest.is_readable()); + /// assert!(w_interest.is_writable()); + /// + /// // Removing all interests from the set returns `None`. + /// assert_eq!(w_interest.remove(Interest::WRITABLE), None); + /// + /// // Remove all interests at once. + /// assert_eq!(RW_INTEREST.remove(RW_INTEREST), None); + /// ``` + pub fn remove(self, other: Interest) -> Option { + let value = self.0 & !other.0; + + if value != 0 { + Some(Self(value)) + } else { + None + } + } + // This function must be crate-private to avoid exposing a `mio` dependency. pub(crate) fn to_mio(self) -> mio::Interest { fn mio_add(wrapped: &mut Option, add: mio::Interest) {