use crate::bitarray::DefaultBitArray;
use crate::bitarray::traits::{BitArrayAccess, BitArrayConstruction, BitArrayConversion};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StaticBitArray<const N: usize> {
bits: [bool; N],
}
impl<const N: usize> StaticBitArray<N> {
pub const fn new(bits: [bool; N]) -> Self {
Self { bits }
}
pub const fn bits(&self) -> &[bool; N] {
&self.bits
}
}
impl<const N: usize> BitArrayAccess for StaticBitArray<N> {
fn iter_bits(&self) -> impl ExactSizeIterator<Item = bool> + DoubleEndedIterator {
self.bits.iter().copied()
}
fn len(&self) -> usize {
N
}
fn get(&self, index: usize) -> Option<bool> {
self.bits.get(index).copied()
}
}
impl<const N: usize> BitArrayConversion for StaticBitArray<N> {
fn to_bits(&self) -> Vec<bool> {
self.bits.to_vec()
}
fn to_bytes(&self) -> Vec<u8> {
let n_bytes = N.div_ceil(8);
let mut bytes = vec![0u8; n_bytes];
for (i, &bit) in self.bits.iter().enumerate() {
if bit {
bytes[i / 8] |= 1 << (i % 8);
}
}
bytes
}
fn convert_to<B: BitArrayConstruction + 'static>(self) -> B
where
Self: Sized,
{
B::from_bits(&self.bits)
}
}
impl<const N: usize> From<&StaticBitArray<N>> for DefaultBitArray {
fn from(a: &StaticBitArray<N>) -> Self {
DefaultBitArray::from_bits(&a.bits)
}
}
impl<const N: usize> From<StaticBitArray<N>> for DefaultBitArray {
fn from(a: StaticBitArray<N>) -> Self {
DefaultBitArray::from_bits(&a.bits)
}
}