symphonia-wem 0.1.0

Symphonia demuxer for Wwise Encoded Media files
Documentation
//! Bits readers and writers.

use bitvec::{
    field::BitField as _, macros::internal::funty::Integral, order::Lsb0, slice::BitSlice,
    store::BitStore, vec::BitVec, view::BitView as _,
};

/// Read bits from bit slice and return the rest of the slice.
#[inline]
pub fn read<I, T>(slice: &BitSlice<T, Lsb0>, bits: usize) -> (&BitSlice<T, Lsb0>, I)
where
    I: Integral,
    T: BitStore,
{
    let result = slice[..bits].load_le();

    (&slice[bits..], result)
}

/// Read bits from bit slice, write them to a bit vec and return the rest.
#[inline]
pub fn read_write<'a, I, T>(
    slice: &'a BitSlice<T, Lsb0>,
    out: &mut BitVec<T, Lsb0>,
    bits: usize,
) -> (&'a BitSlice<T, Lsb0>, I)
where
    I: Integral + BitStore,
    T: BitStore,
{
    let result: I = slice[..bits].load_le();

    write(result, out, bits);

    (&slice[bits..], result)
}

/// Write the number to the bitvec.
#[inline]
pub fn write<I, T>(value: I, out: &mut BitVec<T, Lsb0>, bits: usize)
where
    I: Integral + BitStore,
    T: BitStore,
{
    out.extend(&value.view_bits::<Lsb0>()[..bits]);
}

/// Read a single bool from the bit slice and return the rest.
#[inline]
pub fn read_bool<T>(slice: &BitSlice<T, Lsb0>) -> (&BitSlice<T, Lsb0>, bool)
where
    T: BitStore,
{
    let result: u8 = slice[..1].load_le();

    (&slice[1..], result != 0)
}

/// Read bool from bit slice, write them to a bit vec and return the rest.
#[inline]
pub fn read_write_bool<'a, T>(
    slice: &'a BitSlice<T, Lsb0>,
    out: &mut BitVec<T, Lsb0>,
) -> (&'a BitSlice<T, Lsb0>, bool)
where
    T: BitStore,
{
    let result: u8 = slice[..1].load_le();
    let result = result != 0;

    out.push(result);

    (&slice[1..], result)
}