pub(crate) const UNCOUNTED: bool = false;
pub(crate) const COUNTED: bool = true;
pub(crate) struct Mask(usize);
impl Mask {
pub(crate) const fn new() -> Self {
Self(0)
}
pub(crate) fn reset<const COUNTED: bool>(&mut self) {
if COUNTED {
self.0 += 2;
} else {
self.0 &= 0b1;
}
}
pub(crate) fn is_closed<const COUNTED: bool>(&self) -> bool {
if COUNTED {
self.0 & 0b1 == 1
} else {
self.0 == 1
}
}
pub(crate) fn close<const COUNTED: bool>(&mut self) {
if COUNTED {
self.0 |= 0b1;
} else {
self.0 = 1;
}
}
pub(crate) fn increase_sender_count(&mut self) {
self.0 =
self.0.checked_add(2).expect("cloning the sender would overflow the reference counter");
}
#[must_use = "must react to final sender being dropped"]
pub(crate) fn decrease_sender_count(&mut self) -> bool {
self.0 -= 2;
if self.0 < 2 {
return self.set_closed_bit();
}
false
}
#[cold]
pub(crate) fn set_closed_bit(&mut self) -> bool {
self.0 = 1;
true
}
}