bit-buf 0.1.0

I needed this.
Documentation
use crate::{Storage, StorageMut};

/// The main structure
///
/// Keeps track of the data storage and a cursor for automatic operations
#[derive(Debug)]
pub struct BitBuf<S> {
  data: S,
  pos: usize,
}

impl<S> BitBuf<S> {
  /// Construct from provided data storage
  pub const fn from(data: S) -> Self {
    Self { data, pos: 0 }
  }

  /// Get the underlying bytes from the storage
  #[inline(always)]
  pub fn bytes(&self) -> &[u8]
  where
    S: Storage,
  {
    self.data.as_bytes()
  }

  /// Get the underlying bytes from the storage
  #[inline(always)]
  pub fn bytes_mut(&mut self) -> &mut [u8]
  where
    S: StorageMut,
  {
    self.data.as_bytes_mut()
  }

  /// Advance the internal cursor for automatic operations
  ///
  /// <div class="warning">
  ///
  /// This does not enforce that the cursor stay within storage bounds
  ///
  /// </div>
  #[inline(always)]
  pub fn advance(&mut self, bits: usize) -> &mut Self {
    self.pos += bits;
    self
  }

  /// Set the internal cursor for automatic operations
  ///
  /// <div class="warning">
  ///
  /// This does not enforce that the cursor stay within storage bounds
  ///
  /// </div>
  #[inline(always)]
  pub fn seek(&mut self, offset: usize) -> &mut Self {
    self.pos = offset;
    self
  }

  /// Checks if the internal cursor for automatic operations is aligned on the start of a byte
  ///
  /// <div class="warning">
  ///
  /// This does not care whether the cursor is outside storage bounds
  ///
  /// </div>
  #[inline(always)]
  pub fn is_aligned(&self) -> bool {
    self.pos.is_multiple_of(8)
  }
}