use core::ops::BitOr;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Interest(u8);
impl Interest {
pub const READABLE: Interest = Interest(1);
pub const WRITABLE: Interest = Interest(2);
pub fn is_readable(self) -> bool {
self.0 & 1 != 0
}
pub fn is_writable(self) -> bool {
self.0 & 2 != 0
}
}
impl BitOr for Interest {
type Output = Interest;
fn bitor(self, other: Interest) -> Interest {
Interest(self.0 | other.0)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Ready(u8);
impl Ready {
pub const EMPTY: Ready = Ready(0);
pub const READABLE: Ready = Ready(1);
pub const WRITABLE: Ready = Ready(2);
pub const READ_CLOSED: Ready = Ready(4);
pub const WRITE_CLOSED: Ready = Ready(8);
pub fn is_empty(self) -> bool {
self.0 == 0
}
pub fn is_readable(self) -> bool {
self.0 & (Self::READABLE.0 | Self::READ_CLOSED.0) != 0
}
pub fn is_writable(self) -> bool {
self.0 & (Self::WRITABLE.0 | Self::WRITE_CLOSED.0) != 0
}
pub fn is_read_closed(self) -> bool {
self.0 & Self::READ_CLOSED.0 != 0
}
pub fn is_write_closed(self) -> bool {
self.0 & Self::WRITE_CLOSED.0 != 0
}
}
impl BitOr for Ready {
type Output = Ready;
fn bitor(self, other: Ready) -> Ready {
Ready(self.0 | other.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn interests_and_readiness_combine() {
let both = Interest::READABLE | Interest::WRITABLE;
assert!(both.is_readable() && both.is_writable());
assert!(!Interest::READABLE.is_writable());
let ready = Ready::READABLE | Ready::READ_CLOSED;
assert!(ready.is_readable() && ready.is_read_closed() && !ready.is_writable());
assert!(Ready::EMPTY.is_empty() && !ready.is_empty());
assert!(Ready::WRITE_CLOSED.is_writable());
}
}