use std::fmt;
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct SlotMetadata(u8);
impl SlotMetadata {
const OCCUPIED_BIT: u8 = 0;
const CONTINUATION_BIT: u8 = 1;
const SHIFTED_BIT: u8 = 2;
#[inline]
pub const fn new() -> Self {
return Self(0);
}
#[inline]
pub const fn with_flags(is_occupied: bool, is_continuation: bool, is_shifted: bool) -> Self {
let mut value = 0u8;
if is_occupied {
value |= 1 << Self::OCCUPIED_BIT;
}
if is_continuation {
value |= 1 << Self::CONTINUATION_BIT;
}
if is_shifted {
value |= 1 << Self::SHIFTED_BIT;
}
return Self(value);
}
#[inline]
pub const fn is_empty(&self) -> bool {
return self.0 == 0;
}
#[inline]
pub const fn is_occupied(&self) -> bool {
return (self.0 >> Self::OCCUPIED_BIT) & 1 == 1;
}
#[inline]
pub fn set_occupied(&mut self, value: bool){
if value {
self.0 |= 1 << Self::OCCUPIED_BIT;
} else {
self.0 &= !(1 << Self::OCCUPIED_BIT);
}
}
#[inline]
pub const fn is_continuation(&self) -> bool {
return (self.0 >> Self::CONTINUATION_BIT) & 1 == 1;
}
#[inline]
pub const fn set_continuation(&mut self, value: bool){
if value {
self.0 |= 1 << Self::CONTINUATION_BIT;
} else{
self.0 &= !(1 << Self::CONTINUATION_BIT);
}
}
#[inline]
pub const fn is_shifted(&self) -> bool {
return (self.0 >> Self::SHIFTED_BIT) & 1 == 1;
}
pub const fn set_shifted(&mut self, value: bool){
if value{
self.0 |= 1 << Self::SHIFTED_BIT;
} else {
self.0 &= !(1 << Self::SHIFTED_BIT);
}
}
#[inline]
pub const fn is_cluster_start(&self) -> bool{
return self.is_occupied() && !self.is_shifted();
}
#[inline]
pub const fn is_run_start(&self) -> bool {
return !self.is_continuation() && (self.is_occupied() || self.is_shifted());
}
#[inline]
pub const fn has_data(&self) -> bool {
return self.is_shifted() || self.is_continuation() || self.is_occupied();
}
#[inline]
pub fn clear(&mut self) {
self.0 = 0
}
pub const fn raw(&self) -> u8 {
return self.0;
}
pub const fn from_raw(value: u8) -> Self {
return Self(value);
}
}
impl fmt::Debug for SlotMetadata {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,
"SlotMetadata {{ occupied: {}, continuation: {}, shifted: {}}}",
self.is_occupied(),
self.is_continuation(),
self.is_shifted()
)
}
}
impl fmt::Display for SlotMetadata {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result{
write!(f, "{}{}{}",
if self.is_occupied() {"O"} else {"-"},
if self.is_continuation() {"C"} else {"-"},
if self.is_shifted() {"S"} else {"-"},
)
}
}