use crate::{Storage, StorageMut};
#[derive(Debug)]
pub struct BitBuf<S> {
data: S,
pos: usize,
}
impl<S> BitBuf<S> {
pub const fn from(data: S) -> Self {
Self { data, pos: 0 }
}
#[inline(always)]
pub fn bytes(&self) -> &[u8]
where
S: Storage,
{
self.data.as_bytes()
}
#[inline(always)]
pub fn bytes_mut(&mut self) -> &mut [u8]
where
S: StorageMut,
{
self.data.as_bytes_mut()
}
#[inline(always)]
pub fn advance(&mut self, bits: usize) -> &mut Self {
self.pos += bits;
self
}
#[inline(always)]
pub fn seek(&mut self, offset: usize) -> &mut Self {
self.pos = offset;
self
}
#[inline(always)]
pub fn is_aligned(&self) -> bool {
self.pos.is_multiple_of(8)
}
}