use crate::{Storage, StorageMut};
#[derive(Debug)]
pub struct BitBuf<S> {
data: S,
cursor: usize,
}
impl<S> BitBuf<S> {
pub const fn from(data: S) -> Self {
Self { data, cursor: 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 pos(&self) -> usize {
self.cursor
}
#[inline(always)]
pub fn byte_pos(&self) -> usize {
self.cursor / 8
}
#[inline(always)]
pub fn advance(&mut self, bits: usize) -> &mut Self {
self.cursor += bits;
self
}
#[inline(always)]
pub fn advance_bytes(&mut self, bytes: usize) -> &mut Self {
self.advance(bytes * 8)
}
#[inline(always)]
pub fn seek(&mut self, offset: usize) -> &mut Self {
self.cursor = offset;
self
}
#[inline(always)]
pub fn seek_byte(&mut self, byte: usize) -> &mut Self {
self.seek(byte * 8)
}
#[inline(always)]
pub fn is_aligned(&self) -> bool {
self.cursor.is_multiple_of(8)
}
}