// These must be unique. // same as mio const READABLE: usize = 0b0001; const WRITABLE: usize = 0b0010; // The following are not available on all platforms. #[cfg(target_os = "freebsd")] const AIO: usize = 0b0100; #[cfg(target_os = "freebsd")] const LIO: usize = 0b1000; #[cfg(any(target_os = "linux", target_os = "android"))] const PRIORITY: usize = 0b0001_0000; // error is available on all platforms, but behavior is platform-specific // mio does not have this interest const ERROR: usize = 0b0010_0000;
/// Readiness event interest. /// /// Specifies the readiness events the caller is interested in when awaiting on /// I/O resource readiness states. #[cfg_attr(docsrs, doc(cfg(feature = "net")))] #[derive(Clone, Copy, Eq, PartialEq)] pubstruct Interest(usize);
impl Interest { // The non-FreeBSD definitions in this block are active only when // building documentation.
cfg_aio! { /// Interest for POSIX AIO. #[cfg(target_os = "freebsd")] pubconst AIO: Interest = Interest(AIO);
/// Interest in all readable events. /// /// Readable interest includes read-closed events. pubconst READABLE: Interest = Interest(READABLE);
/// Interest in all writable events. /// /// Writable interest includes write-closed events. pubconst WRITABLE: Interest = Interest(WRITABLE);
/// Interest in error events. /// /// Passes error interest to the underlying OS selector. /// Behavior is platform-specific, read your platform's documentation. pubconst ERROR: Interest = Interest(ERROR);
/// Returns true if the value includes priority interest. /// /// # Examples /// /// ``` /// use tokio::io::Interest; /// /// assert!(!Interest::READABLE.is_priority()); /// assert!(Interest::PRIORITY.is_priority()); /// /// let both = Interest::READABLE | Interest::PRIORITY; /// assert!(both.is_priority()); /// ``` #[cfg(any(target_os = "linux", target_os = "android"))] #[cfg_attr(docsrs, doc(cfg(any(target_os = "linux", target_os = "android"))))] pubconstfn 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()); #[must_use = "this returns the result of the operation, without modifying the original"] pubconstfn add(self, other: Interest) -> 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"] pubfn remove(self, other: Interest) -> Option<Interest> { 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) { match wrapped {
Some(inner) => *inner |= add,
None => *wrapped = Some(add),
}
}
// mio does not allow and empty interest, so use None for empty letmut mio = None;
ifself.is_error() { // There is no error interest in mio, because error events are always reported. // But mio interests cannot be empty and an interest is needed just for the registration. // // read readiness is filtered out in `Interest::mask` or `Ready::from_interest` if // the read interest was not specified by the user.
mio_add(&mut mio, mio::Interest::READABLE);
}
// the default `mio::Interest::READABLE` should never be used in practice. Either // // - at least one tokio interest with a mio counterpart was used // - only the error tokio interest was specified // // in both cases, `mio` is Some already
mio.unwrap_or(mio::Interest::READABLE)
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.