#[cfg(test)]
#[path = "../../tests/unit/utils/types_test.rs"]
mod types_test;
use std::fmt;
use std::fmt::Formatter;
use std::ops::ControlFlow;
pub trait UnwrapValue {
type Value;
fn unwrap_value(self) -> Self::Value;
}
impl<T> UnwrapValue for ControlFlow<T, T> {
type Value = T;
fn unwrap_value(self) -> Self::Value {
match self {
ControlFlow::Continue(value) => value,
ControlFlow::Break(value) => value,
}
}
}
pub fn short_type_name<T: ?Sized>() -> &'static str {
let name = std::any::type_name::<T>();
name.rsplit_once(':').map(|(_, name)| name).unwrap_or(name)
}
pub struct FixedBitArray<const N: usize> {
data: [u8; N],
}
impl<const N: usize> Default for FixedBitArray<N> {
fn default() -> Self {
Self { data: [0; N] }
}
}
impl<const N: usize> fmt::Binary for FixedBitArray<N> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
for (count, n) in self.data.iter().enumerate() {
if count != 0 {
write!(f, " ")?;
}
write!(f, "{:08b}", n.reverse_bits())?;
}
Ok(())
}
}
impl<const N: usize> FixedBitArray<N> {
pub fn set(&mut self, index: usize, value: bool) -> bool {
if index >= N * 8 {
return false;
}
let byte_index = index / 8;
let bit_index = index % 8;
if value {
self.data[byte_index] |= 1 << bit_index;
} else {
self.data[byte_index] &= !(1 << bit_index);
}
true
}
pub fn get(&self, index: usize) -> bool {
if index >= N * 8 {
return false;
}
let byte_index = index / 8;
let bit_index = index % 8;
(self.data[byte_index] & (1 << bit_index)) != 0
}
pub fn replace(&mut self, index: usize, new_value: bool) -> bool {
if index >= N * 8 {
return false;
}
let byte_index = index / 8;
let bit_index = index % 8;
let shift = 1 << bit_index;
let old_value = (self.data[byte_index] & shift) != 0;
if new_value {
self.data[byte_index] |= shift;
} else {
self.data[byte_index] &= !shift;
}
old_value
}
}