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
use crate::bitcursor::{BitCursor, BitCursorResult};
use byteorder::{ReadBytesExt, NetworkEndian};
use ux::*;
pub trait BitRead: Sized {
fn bit_read(cursor: &mut BitCursor) -> BitCursorResult<Self>;
}
impl BitRead for bool {
fn bit_read(cursor: &mut BitCursor) -> BitCursorResult<Self> {
Ok(cursor.read_bit()? == 1)
}
}
macro_rules! impl_bit_read {
($num_bits:expr,$type:ty,u8) => {
impl BitRead for $type {
fn bit_read(cursor: &mut BitCursor) -> BitCursorResult<Self> {
Ok(<$type>::new(cursor.read_bits_as_u8($num_bits)?))
}
}
};
($num_bits:expr,$type:ty,u16) => {
impl BitRead for $type {
fn bit_read(cursor: &mut BitCursor) -> BitCursorResult<Self> {
Ok(<$type>::new(cursor.read_bits_as_u16($num_bits)?))
}
}
};
($num_bits:expr,$type:ty,u32) => {
impl BitRead for $type {
fn bit_read(cursor: &mut BitCursor) -> BitCursorResult<Self> {
Ok(<$type>::new(cursor.read_bits_as_u32($num_bits)?))
}
}
};
}
impl_bit_read!(2, u2, u8);
impl_bit_read!(3, u3, u8);
impl_bit_read!(4, u4, u8);
impl_bit_read!(5, u5, u8);
impl_bit_read!(6, u6, u8);
impl_bit_read!(7, u7, u8);
impl_bit_read!(9, u9, u16);
impl_bit_read!(10, u10, u16);
impl_bit_read!(11, u11, u16);
impl_bit_read!(12, u12, u16);
impl_bit_read!(13, u13, u16);
impl_bit_read!(14, u14, u16);
impl_bit_read!(15, u15, u16);
impl_bit_read!(17, u17, u32);
impl_bit_read!(18, u18, u32);
impl_bit_read!(19, u19, u32);
impl_bit_read!(20, u20, u32);
impl_bit_read!(21, u21, u32);
impl_bit_read!(22, u22, u32);
impl_bit_read!(23, u23, u32);
impl_bit_read!(24, u24, u32);
impl_bit_read!(25, u25, u32);
impl_bit_read!(26, u26, u32);
impl_bit_read!(27, u27, u32);
impl_bit_read!(28, u28, u32);
impl_bit_read!(29, u29, u32);
impl_bit_read!(30, u30, u32);
impl_bit_read!(31, u31, u32);
impl BitRead for u8 {
fn bit_read(cursor: &mut BitCursor) -> BitCursorResult<Self> {
ReadBytesExt::read_u8(cursor).map_err(std::io::Error::into)
}
}
impl BitRead for u16 {
fn bit_read(cursor: &mut BitCursor) -> BitCursorResult<Self> {
ReadBytesExt::read_u16::<NetworkEndian>(cursor).map_err(std::io::Error::into)
}
}
impl BitRead for u32 {
fn bit_read(cursor: &mut BitCursor) -> BitCursorResult<Self> {
ReadBytesExt::read_u32::<NetworkEndian>(cursor).map_err(std::io::Error::into)
}
}
impl BitRead for u128 {
fn bit_read(cursor: &mut BitCursor) -> BitCursorResult<Self> {
ReadBytesExt::read_u128::<NetworkEndian>(cursor).map_err(std::io::Error::into)
}
}