use crate::driver::sys::event::{ready_as_usize, ready_from_usize, Ready};
use std::fmt;
use std::ops;
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord)]
pub struct UnixReady(Ready);
const ERROR: usize = 0b00_0100;
const HUP: usize = 0b00_1000;
const LIO: usize = 0b00_0000;
const PRI: usize = 0b100_0000;
pub const READY_ALL: usize = ERROR | HUP | LIO | PRI;
#[test]
fn test_ready_all() {
let readable = Ready::readable().as_usize();
let writable = Ready::writable().as_usize();
assert_eq!(
READY_ALL | readable | writable,
ERROR + HUP + LIO + PRI + readable + writable
);
assert!(!Ready::from(UnixReady::priority()).is_writable());
}
impl UnixReady {
#[inline]
pub fn error() -> UnixReady {
UnixReady(ready_from_usize(ERROR))
}
#[inline]
pub fn hup() -> UnixReady {
UnixReady(ready_from_usize(HUP))
}
pub fn priority() -> UnixReady {
UnixReady(ready_from_usize(PRI))
}
#[inline]
pub fn is_error(&self) -> bool {
self.contains(ready_from_usize(ERROR))
}
#[inline]
pub fn is_hup(&self) -> bool {
self.contains(ready_from_usize(HUP))
}
#[inline]
pub fn is_priority(&self) -> bool {
self.contains(ready_from_usize(PRI))
}
}
impl From<Ready> for UnixReady {
fn from(src: Ready) -> UnixReady {
UnixReady(src)
}
}
impl From<UnixReady> for Ready {
fn from(src: UnixReady) -> Ready {
src.0
}
}
impl ops::Deref for UnixReady {
type Target = Ready;
fn deref(&self) -> &Ready {
&self.0
}
}
impl ops::DerefMut for UnixReady {
fn deref_mut(&mut self) -> &mut Ready {
&mut self.0
}
}
impl ops::BitOr for UnixReady {
type Output = UnixReady;
#[inline]
fn bitor(self, other: UnixReady) -> UnixReady {
(self.0 | other.0).into()
}
}
impl ops::BitXor for UnixReady {
type Output = UnixReady;
#[inline]
fn bitxor(self, other: UnixReady) -> UnixReady {
(self.0 ^ other.0).into()
}
}
impl ops::BitAnd for UnixReady {
type Output = UnixReady;
#[inline]
fn bitand(self, other: UnixReady) -> UnixReady {
(self.0 & other.0).into()
}
}
impl ops::Sub for UnixReady {
type Output = UnixReady;
#[inline]
fn sub(self, other: UnixReady) -> UnixReady {
ready_from_usize(ready_as_usize(self.0) & !ready_as_usize(other.0)).into()
}
}
#[doc(hidden)]
impl ops::Not for UnixReady {
type Output = UnixReady;
#[inline]
fn not(self) -> UnixReady {
(!self.0).into()
}
}
impl fmt::Debug for UnixReady {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let mut one = false;
let flags = [
(UnixReady(Ready::readable()), "Readable"),
(UnixReady(Ready::writable()), "Writable"),
(UnixReady::error(), "Error"),
(UnixReady::hup(), "Hup"),
(UnixReady::priority(), "Priority"),
];
for &(flag, msg) in &flags {
if self.contains(flag) {
if one {
write!(fmt, " | ")?
}
write!(fmt, "{}", msg)?;
one = true
}
}
if !one {
fmt.write_str("(empty)")?;
}
Ok(())
}
}