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
use crate::{Limb, LimbUInt, UInt};
#[derive(Clone, Debug)]
pub(super) struct Decoder<const LIMBS: usize> {
limbs: [Limb; LIMBS],
index: usize,
bytes: usize,
}
impl<const LIMBS: usize> Decoder<LIMBS> {
pub const fn new() -> Self {
Self {
limbs: [Limb::ZERO; LIMBS],
index: 0,
bytes: 0,
}
}
pub const fn add_byte(mut self, byte: u8) -> Self {
if self.bytes == Limb::BYTE_SIZE {
const_assert!(self.index < LIMBS, "too many bytes in UInt");
self.index += 1;
self.bytes = 0;
}
self.limbs[self.index].0 |= (byte as LimbUInt) << (self.bytes * 8);
self.bytes += 1;
self
}
pub const fn finish(self) -> UInt<LIMBS> {
const_assert!(self.index == LIMBS - 1, "decoded UInt is missing limbs");
const_assert!(
self.bytes == Limb::BYTE_SIZE,
"decoded UInt is missing bytes"
);
UInt { limbs: self.limbs }
}
}
impl<const LIMBS: usize> Default for Decoder<LIMBS> {
fn default() -> Self {
Self::new()
}
}