Repository: tokio-rs/tokio
File: tokio/src/io/interest.rs (Rust)

Code before:
    #[cfg_attr(docsrs, doc(cfg(any(target_os = "linux", target_os = "android"))))]
    pub const fn is_priority(self) -> bool {
        self.0 & PRIORITY != 0
    }

    /// Add together two `Interest` values.
    ///
    /// This function works from a `const` context.
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio::io::Interest;
    ///
    /// const BOTH: Interest = Interest::READABLE.add(Interest::WRITABLE);
    ///
    /// assert!(BOTH.is_readable());
    /// assert!(BOTH.is_writable());
    pub const fn add(self, other: Interest) -> Interest {
        Self(self.0 | other.0)
    }

    // This function must be crate-private to avoid exposing a `mio` dependency.
    pub(crate) fn to_mio(self) -> mio::Interest {
        // mio does not allow and empty interest, so pick an arbitrary starting value
        let mut mio = mio::Interest::READABLE;

        if self.is_readable() {
            mio |= mio::Interest::READABLE;
        }

        if self.is_writable() {
            mio |= mio::Interest::WRITABLE;
        }

        #[cfg(any(target_os = "linux", target_os = "android"))]
        if self.is_priority() {
            mio |= mio::Interest::PRIORITY;
        }

        #[cfg(target_os = "freebsd")]
        if self.is_aio() {
            mio |= mio::Interest::AIO;
        }

        #[cfg(target_os = "freebsd")]
        if self.is_lio() {
            mio |= mio::Interest::LIO;
        }

        // Finally remove the starting value unless either:

Review comment (suggestion):
```suggestion
    pub(crate) fn to_mio(self) -> mio::Interest {
    	fn mio_add(wrapped: &mut Option<mio::Interest>, add: mio::Interest) {
	    match wrapped {
	        Some(inner) => *inner |= add,
		None => *wrapped = Some(add),
	    }
	}

        // mio does not allow and empty interest, so use None for empty
        let mut mio = None;
```


Code after:
    #[cfg_attr(docsrs, doc(cfg(any(target_os = "linux", target_os = "android"))))]
    pub const fn is_priority(self) -> bool {
        self.0 & PRIORITY != 0
    }

    /// Add together two `Interest` values.
    ///
    /// This function works from a `const` context.
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio::io::Interest;
    ///
    /// const BOTH: Interest = Interest::READABLE.add(Interest::WRITABLE);
    ///
    /// assert!(BOTH.is_readable());
    /// assert!(BOTH.is_writable());
    pub const fn add(self, other: Interest) -> Interest {
        Self(self.0 | other.0)
    }

    // 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) {
            match wrapped {
                Some(inner) => *inner |= add,
                None => *wrapped = Some(add),
            }
        }

        // mio does not allow and empty interest, so use None for empty
        let mut mio = None;

        if self.is_readable() {
            mio_add(&mut mio, mio::Interest::READABLE);
        }

        if self.is_writable() {
            mio_add(&mut mio, mio::Interest::WRITABLE);
        }

        #[cfg(any(target_os = "linux", target_os = "android"))]
        if self.is_priority() {
            mio_add(&mut mio, mio::Interest::PRIORITY);
        }

        #[cfg(target_os = "freebsd")]
        if self.is_aio() {
            mio_add(&mut mio, mio::Interest::AIO);
        }