use std::ops::{BitAnd, BitOr, BitOrAssign, Not};
#[derive(Clone, Copy, PartialEq, Eq, Default, Hash)]
pub struct Dirty(u8);
impl Dirty {
pub const NONE: Dirty = Dirty(0);
pub const STYLE: Dirty = Dirty(1 << 0);
pub const LAYOUT: Dirty = Dirty(1 << 1);
pub const PAINT: Dirty = Dirty(1 << 2);
pub const BINDINGS: Dirty = Dirty(1 << 3);
pub const REBUILD: Dirty = Dirty(1 << 4);
pub const fn bits(self) -> u8 {
self.0
}
pub const fn contains(self, other: Dirty) -> bool {
self.0 & other.0 == other.0
}
pub const fn intersects(self, other: Dirty) -> bool {
self.0 & other.0 != 0
}
pub const fn is_empty(self) -> bool {
self.0 == 0
}
pub fn insert(&mut self, other: Dirty) {
self.0 |= other.0;
}
pub fn remove(&mut self, other: Dirty) {
self.0 &= !other.0;
}
pub fn take(&mut self) -> Dirty {
std::mem::take(self)
}
}
impl BitOr for Dirty {
type Output = Dirty;
fn bitor(self, rhs: Dirty) -> Dirty {
Dirty(self.0 | rhs.0)
}
}
impl BitOrAssign for Dirty {
fn bitor_assign(&mut self, rhs: Dirty) {
self.0 |= rhs.0;
}
}
impl BitAnd for Dirty {
type Output = Dirty;
fn bitand(self, rhs: Dirty) -> Dirty {
Dirty(self.0 & rhs.0)
}
}
impl Not for Dirty {
type Output = Dirty;
fn not(self) -> Dirty {
Dirty(!self.0)
}
}
impl std::fmt::Debug for Dirty {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let names = [
(Dirty::STYLE, "STYLE"),
(Dirty::LAYOUT, "LAYOUT"),
(Dirty::PAINT, "PAINT"),
(Dirty::BINDINGS, "BINDINGS"),
(Dirty::REBUILD, "REBUILD"),
];
let set: Vec<_> = names
.iter()
.filter(|(d, _)| self.contains(*d))
.map(|(_, n)| *n)
.collect();
write!(f, "Dirty({})", set.join("|"))
}
}