1pub struct Bytes<'a> {
2 data: &'a [u8],
3 pos: usize,
4}
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum Error {
9 NotEnoughBuffer { requested: usize, available: usize },
11}
12
13impl Error {
14 #[inline]
15 fn not_enough_buffer(requested: usize, bytes: &Bytes) -> Self {
16 Self::NotEnoughBuffer {
17 requested,
18 available: bytes.remaining(),
19 }
20 }
21}
22
23impl core::fmt::Display for Error {
24 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
25 match self {
26 Self::NotEnoughBuffer {
27 requested,
28 available,
29 } => write!(
30 f,
31 "Attempted to read {requested} bytes but only {available} available.",
32 ),
33 }
34 }
35}
36
37impl<'a> Bytes<'a> {
38 pub fn new(data: &'a [u8]) -> Self {
39 Self { data, pos: 0 }
40 }
41
42 #[inline]
43 fn remaining(&self) -> usize {
44 self.data.len() - self.pos
45 }
46
47 #[inline]
48 pub fn remaining_bytes(&self) -> &'a [u8] {
49 &self.data[self.pos..]
50 }
51
52 #[inline]
56 pub fn get_bytes(&mut self, count: usize) -> Result<&[u8], Error> {
57 let bytes = &self
58 .data
59 .get(self.pos..(self.pos + count))
60 .ok_or_else(|| Error::not_enough_buffer(count, self))?;
61 self.pos += count;
62 Ok(bytes)
63 }
64
65 #[inline]
69 pub fn get_array<const SIZE: usize>(&mut self) -> Result<[u8; SIZE], Error> {
70 let bytes = self.get_bytes(SIZE)?;
71 let mut arr = [0u8; SIZE];
72
73 arr.copy_from_slice(bytes);
74
75 debug_assert_eq!(arr.as_slice(), bytes);
76
77 Ok(arr)
78 }
79
80 #[inline]
84 pub fn get_u8(&mut self) -> Result<u8, Error> {
85 let val = *self
86 .data
87 .get(self.pos)
88 .ok_or_else(|| Error::not_enough_buffer(1, self))?;
89 self.pos += 1;
90 Ok(val)
91 }
92
93 #[inline]
97 pub fn get_i8(&mut self) -> Result<i8, Error> {
98 let val = *self
99 .data
100 .get(self.pos)
101 .ok_or_else(|| Error::not_enough_buffer(1, self))? as i8;
102 self.pos += 1;
103 Ok(val)
104 }
105
106 #[inline]
110 pub fn get_u16_le(&mut self) -> Result<u16, Error> {
111 Ok(u16::from_le_bytes(self.get_array()?))
112 }
113
114 #[inline]
118 pub fn get_i16_le(&mut self) -> Result<i16, Error> {
119 Ok(i16::from_le_bytes(self.get_array()?))
120 }
121
122 #[inline]
126 pub fn get_u24_le(&mut self) -> Result<u32, Error> {
127 const SIZE: usize = 3;
128
129 let mut val = [0u8; SIZE + 1];
130 val[..3].copy_from_slice(
131 self.data
132 .get(self.pos..self.pos + SIZE)
133 .ok_or_else(|| Error::not_enough_buffer(SIZE, self))?,
134 );
135 self.pos += SIZE;
136
137 debug_assert_eq!(val[3], 0);
138 Ok(u32::from_le_bytes(val))
139 }
140
141 #[inline]
145 pub fn get_i24_le(&mut self) -> Result<i32, Error> {
146 const SIZE: usize = 3;
147
148 let mut val = [0u8; SIZE + 1];
149 val[..3].copy_from_slice(
150 self.data
151 .get(self.pos..self.pos + SIZE)
152 .ok_or_else(|| Error::not_enough_buffer(SIZE, self))?,
153 );
154 self.pos += SIZE;
155
156 debug_assert_eq!(val[3], 0);
157 Ok(i32::from_le_bytes(val))
158 }
159
160 #[inline]
164 pub fn get_u32_le(&mut self) -> Result<u32, Error> {
165 Ok(u32::from_le_bytes(self.get_array()?))
166 }
167
168 #[inline]
172 pub fn get_i32_le(&mut self) -> Result<i32, Error> {
173 Ok(i32::from_le_bytes(self.get_array()?))
174 }
175
176 #[inline]
180 pub fn get_u64_le(&mut self) -> Result<u64, Error> {
181 Ok(u64::from_le_bytes(self.get_array()?))
182 }
183
184 #[inline]
188 pub fn get_i64_le(&mut self) -> Result<i64, Error> {
189 Ok(i64::from_le_bytes(self.get_array()?))
190 }
191
192 #[inline]
196 pub fn get_f32_le(&mut self) -> Result<f32, Error> {
197 Ok(f32::from_le_bytes(self.get_array()?))
198 }
199
200 #[inline]
204 pub fn get_f64_le(&mut self) -> Result<f64, Error> {
205 Ok(f64::from_le_bytes(self.get_array()?))
206 }
207}