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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use bitvec::{order::Msb0, vec::BitVec, view::AsBits};
use num_bigint::{BigInt, BigUint};

use crate::{
    BitPackAs, BitReader, BitReaderExt, BitUnpackAs, BitWriter, BitWriterExt, Error, NBits,
    VarBytes,
};

impl<const BITS: usize> BitPackAs<BigUint> for NBits<BITS> {
    #[inline]
    fn pack_as<W>(source: &BigUint, mut writer: W) -> Result<(), W::Error>
    where
        W: BitWriter,
    {
        let used_bits = source.bits() as usize;
        if BITS < used_bits {
            return Err(Error::custom(format!(
                "{source:#b} cannot be packed into {BITS} bits"
            )));
        }

        writer.repeat_bit(BITS - used_bits, false)?;

        let bytes = source.to_bytes_be();
        let mut bits = bytes.as_bits::<Msb0>();
        bits = &bits[bits.len() - used_bits..];
        writer.with_bits(bits)?;
        Ok(())
    }
}

impl<const BITS: usize> BitUnpackAs<BigUint> for NBits<BITS> {
    #[inline]
    fn unpack_as<R>(mut reader: R) -> Result<BigUint, R::Error>
    where
        R: BitReader,
    {
        let mut bits = reader.read_bitvec(BITS)?;
        let total_bits = (BITS + 7) & !7;
        bits.resize(total_bits, false);
        bits.shift_right(total_bits - BITS);
        Ok(BigUint::from_bytes_be(bits.as_raw_slice()))
    }
}

impl<const BITS: usize> BitPackAs<BigInt> for NBits<BITS> {
    #[inline]
    fn pack_as<W>(source: &BigInt, mut writer: W) -> Result<(), W::Error>
    where
        W: BitWriter,
    {
        let used_bits = source.bits() as usize;
        if BITS < used_bits {
            return Err(Error::custom(format!(
                "{source:#b} cannot be packed into {BITS} bits"
            )));
        }

        writer.repeat_bit(BITS - used_bits, false)?;

        let bytes = source.to_signed_bytes_be();
        let mut bits = bytes.as_bits::<Msb0>();
        bits = &bits[bits.len() - used_bits..];
        writer.with_bits(bits)?;
        Ok(())
    }
}

impl<const BITS: usize> BitUnpackAs<BigInt> for NBits<BITS> {
    #[inline]
    fn unpack_as<R>(mut reader: R) -> Result<BigInt, R::Error>
    where
        R: BitReader,
    {
        let mut bits = reader.read_bitvec(BITS)?;
        let total_bits = (BITS + 7) & !7;
        bits.resize(total_bits, false);
        bits.shift_right(total_bits - BITS);
        Ok(BigInt::from_signed_bytes_be(bits.as_raw_slice()))
    }
}

pub struct VarUint<const BITS_FOR_BYTES_LEN: usize>;

impl<const BITS_FOR_BYTES_LEN: usize> BitPackAs<BigUint> for VarUint<BITS_FOR_BYTES_LEN> {
    #[inline]
    fn pack_as<W>(source: &BigUint, mut writer: W) -> Result<(), W::Error>
    where
        W: BitWriter,
    {
        writer.pack_as::<_, VarBytes<BITS_FOR_BYTES_LEN>>(source.to_bytes_be())?;
        Ok(())
    }
}

impl<const BITS_FOR_BYTES_LEN: usize> BitUnpackAs<BigUint> for VarUint<BITS_FOR_BYTES_LEN> {
    #[inline]
    fn unpack_as<R>(mut reader: R) -> Result<BigUint, R::Error>
    where
        R: BitReader,
    {
        let mut bits = BitVec::<u8, Msb0>::from_vec(
            reader.unpack_as::<Vec<u8>, VarBytes<BITS_FOR_BYTES_LEN>>()?,
        );
        let total_bits = (bits.len() + 7) & !7;
        let shift = total_bits - bits.len();
        bits.resize(total_bits, false);
        bits.shift_right(shift);
        Ok(BigUint::from_bytes_be(bits.as_raw_slice()))
    }
}

pub struct VarInt<const BITS_FOR_BYTES_LEN: usize>;

impl<const BITS_FOR_BYTES_LEN: usize> BitPackAs<BigInt> for VarInt<BITS_FOR_BYTES_LEN> {
    #[inline]
    fn pack_as<W>(source: &BigInt, mut writer: W) -> Result<(), W::Error>
    where
        W: BitWriter,
    {
        writer.pack_as::<_, VarBytes<BITS_FOR_BYTES_LEN>>(source.to_signed_bytes_be())?;
        Ok(())
    }
}

impl<const BITS_FOR_BYTES_LEN: usize> BitUnpackAs<BigInt> for VarInt<BITS_FOR_BYTES_LEN> {
    #[inline]
    fn unpack_as<R>(mut reader: R) -> Result<BigInt, R::Error>
    where
        R: BitReader,
    {
        let mut bits = BitVec::<u8, Msb0>::from_vec(
            reader.unpack_as::<Vec<u8>, VarBytes<BITS_FOR_BYTES_LEN>>()?,
        );
        let total_bits = (bits.len() + 7) & !7;
        let shift = total_bits - bits.len();
        bits.resize(total_bits, false);
        bits.shift_right(shift);
        Ok(BigInt::from_signed_bytes_be(bits.as_raw_slice()))
    }
}