use crate::sys;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct HtonFlags(u32);
impl HtonFlags {
pub const NONE: Self = Self(0);
pub const CAN_RECREATE: Self = Self(sys::HTON_CAN_RECREATE);
#[must_use]
pub const fn empty() -> Self {
Self(0)
}
#[must_use]
pub const fn bits(self) -> u32 {
self.0
}
#[must_use]
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
impl core::ops::BitOr for HtonFlags {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
self.union(rhs)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_recreate_matches_sys_constant() {
assert_eq!(HtonFlags::CAN_RECREATE.bits(), sys::HTON_CAN_RECREATE);
}
#[test]
fn none_is_empty() {
assert_eq!(HtonFlags::NONE, HtonFlags::empty());
assert_eq!(HtonFlags::empty().bits(), 0);
}
#[test]
fn union_and_contains() {
let f = HtonFlags::NONE | HtonFlags::CAN_RECREATE;
assert!(f.contains(HtonFlags::CAN_RECREATE));
assert!(HtonFlags::NONE.contains(HtonFlags::NONE));
assert!(!HtonFlags::NONE.contains(HtonFlags::CAN_RECREATE));
}
}