Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io: add Interest::remove method #5906

Merged
merged 3 commits into from
Sep 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tokio/src/io/interest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,40 @@ impl Interest {
Self(self.0 | other.0)
}

/// Remove `Interest` from `self`.
///
/// Interests present in `other` but *not* in `self` are ignored.
///
/// 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);
/// ```
#[must_use = "this returns the result of the operation, without modifying the original"]
pub fn remove(self, other: Interest) -> Option<Interest> {
nylonicious marked this conversation as resolved.
Show resolved Hide resolved
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<mio::Interest>, add: mio::Interest) {
Expand Down
Loading