use core::{
fmt,
num::NonZeroU16,
ops::{BitOr, BitOrAssign},
};
const READABLE: u16 = 1;
const WRITABLE: u16 = 1 << 1;
const PRIORITY: u16 = 1 << 2;
const AIO: u16 = 1 << 3;
const LIO: u16 = 1 << 4;
#[repr(transparent)]
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Interest(NonZeroU16);
impl Interest {
pub const READABLE: Self = Self::from_bits(READABLE);
pub const WRITABLE: Self = Self::from_bits(WRITABLE);
pub const READ_WRITE: Self = Self::from_bits(READABLE | WRITABLE);
pub const PRIORITY: Self = Self::from_bits(PRIORITY);
pub const AIO: Self = Self::from_bits(AIO);
pub const LIO: Self = Self::from_bits(LIO);
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self::from_bits(self.0.get() | other.0.get())
}
pub const fn remove(self, other: Self) -> Option<Self> {
match NonZeroU16::new(self.0.get() & !other.0.get()) {
Some(bits) => Some(Self(bits)),
None => None,
}
}
pub const fn contains(self, other: Self) -> bool {
self.0.get() & other.0.get() == other.0.get()
}
pub const fn intersects(self, other: Self) -> bool {
self.0.get() & other.0.get() != 0
}
pub const fn is_readable(self) -> bool {
self.0.get() & READABLE != 0
}
pub const fn is_writable(self) -> bool {
self.0.get() & WRITABLE != 0
}
pub const fn is_priority(self) -> bool {
self.0.get() & PRIORITY != 0
}
pub const fn is_aio(self) -> bool {
self.0.get() & AIO != 0
}
pub const fn is_lio(self) -> bool {
self.0.get() & LIO != 0
}
const fn from_bits(bits: u16) -> Self {
match NonZeroU16::new(bits) {
Some(bits) => Self(bits),
None => panic!("readiness interest must be nonempty"),
}
}
}
impl BitOr for Interest {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
self.union(rhs)
}
}
impl BitOrAssign for Interest {
fn bitor_assign(&mut self, rhs: Self) {
*self = (*self).union(rhs);
}
}
impl fmt::Debug for Interest {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let flags = [
(Self::READABLE, "READABLE"),
(Self::WRITABLE, "WRITABLE"),
(Self::PRIORITY, "PRIORITY"),
(Self::AIO, "AIO"),
(Self::LIO, "LIO"),
];
let mut separator = "";
for (flag, name) in flags {
if self.contains(flag) {
formatter.write_str(separator)?;
formatter.write_str(name)?;
separator = " | ";
}
}
Ok(())
}
}