#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct GroupHandles(u8);
impl GroupHandles {
pub const NONE: Self = Self(0);
pub const EMPTY_SPACE: Self = Self(1);
pub const GRIP: Self = Self(1 << 1);
pub const MODIFIER_DRAG: Self = Self(1 << 2);
pub const MENU: Self = Self(1 << 3);
pub const ALL: Self = Self(0b1111);
#[inline]
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
#[inline]
#[must_use]
pub const fn with(self, flag: Self, on: bool) -> Self {
if on {
Self(self.0 | flag.0)
} else {
Self(self.0 & !flag.0)
}
}
#[inline]
#[must_use]
pub const fn intersection(self, other: Self) -> Self {
Self(self.0 & other.0)
}
}
impl Default for GroupHandles {
fn default() -> Self {
Self::ALL
}
}
impl std::ops::BitOr for GroupHandles {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}
impl std::ops::BitOrAssign for GroupHandles {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
impl std::ops::BitAnd for GroupHandles {
type Output = Self;
#[inline]
fn bitand(self, rhs: Self) -> Self {
Self(self.0 & rhs.0)
}
}
impl std::ops::BitAndAssign for GroupHandles {
#[inline]
fn bitand_assign(&mut self, rhs: Self) {
self.0 &= rhs.0;
}
}
impl std::ops::Not for GroupHandles {
type Output = Self;
#[inline]
fn not(self) -> Self {
Self(!self.0 & Self::ALL.0)
}
}
#[cfg(test)]
mod tests {
use super::GroupHandles;
#[test]
fn flags_combine_and_clear() {
let all = GroupHandles::default();
assert_eq!(all, GroupHandles::ALL);
let no_grip = all.with(GroupHandles::GRIP, false);
assert!(!no_grip.contains(GroupHandles::GRIP));
assert!(no_grip.contains(GroupHandles::EMPTY_SPACE | GroupHandles::MENU));
assert_eq!(!GroupHandles::ALL, GroupHandles::NONE);
assert_eq!(
all & !GroupHandles::MENU,
GroupHandles::EMPTY_SPACE | GroupHandles::GRIP | GroupHandles::MODIFIER_DRAG
);
assert_eq!(
all.intersection(GroupHandles::GRIP),
GroupHandles::GRIP,
"a setting narrows the app's set"
);
}
}