1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use bitvec::{order::Msb0, slice::BitSlice, view::AsBits};

use crate::{BitPack, BitPackAs, BitReader, BitReaderExt, BitUnpackAs, BitWriter, BitWriterExt};

pub struct AsBitSlice;

impl<T> BitPackAs<T> for AsBitSlice
where
    T: AsRef<BitSlice<u8, Msb0>>,
{
    #[inline]
    fn pack_as<W>(source: &T, writer: W) -> Result<(), W::Error>
    where
        W: BitWriter,
    {
        source.as_ref().pack(writer)
    }
}

pub struct AsBytes;

impl<T> BitPackAs<T> for AsBytes
where
    T: AsRef<[u8]>,
{
    #[inline]
    fn pack_as<W>(source: &T, writer: W) -> Result<(), W::Error>
    where
        W: BitWriter,
    {
        source.as_bits().pack(writer)
    }
}

pub struct NBits<const BITS: usize>;

pub struct VarBytes<const BITS_FOR_BYTES_LEN: usize>;

impl<const BITS_FOR_BYTES_LEN: usize, T> BitPackAs<T> for VarBytes<BITS_FOR_BYTES_LEN>
where
    T: AsRef<[u8]> + ?Sized,
{
    #[inline]
    fn pack_as<W>(source: &T, mut writer: W) -> Result<(), W::Error>
    where
        W: BitWriter,
    {
        let source = source.as_ref();
        writer
            .pack_as::<_, NBits<BITS_FOR_BYTES_LEN>>(source.len())?
            .pack_as::<_, AsBytes>(source)?;
        Ok(())
    }
}

impl<const BITS_FOR_BYTES_LEN: usize> BitUnpackAs<Vec<u8>> for VarBytes<BITS_FOR_BYTES_LEN> {
    #[inline]
    fn unpack_as<R>(mut reader: R) -> Result<Vec<u8>, R::Error>
    where
        R: BitReader,
    {
        let num_bytes = reader.unpack_as::<_, NBits<BITS_FOR_BYTES_LEN>>()?;
        reader.read_bytes_vec(num_bytes)
    }
}