mavlink_core/
bytes.rs

1pub struct Bytes<'a> {
2    data: &'a [u8],
3    pos: usize,
4}
5
6/// Simple error types for the bytes module.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum Error {
9    /// Attempted to to read more bytes than available.
10    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    /// # Errors
53    ///
54    /// Will return an error if not at least `count` bytes remain in the buffer
55    #[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    /// # Errors
66    ///
67    /// Will return an error if not at least `SIZE` bytes remain in the buffer
68    #[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    /// # Errors
81    ///
82    /// Will return an error if nothing is remaining in the buffer
83    #[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    /// # Errors
94    ///
95    /// Will return an error if nothing is remaining in the buffer
96    #[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    /// # Errors
107    ///
108    /// Will return an error if less then the 2 required bytes for a `u16` remain
109    #[inline]
110    pub fn get_u16_le(&mut self) -> Result<u16, Error> {
111        Ok(u16::from_le_bytes(self.get_array()?))
112    }
113
114    /// # Errors
115    ///
116    /// Will return an error if less then the 2 required bytes for a `i16` remain
117    #[inline]
118    pub fn get_i16_le(&mut self) -> Result<i16, Error> {
119        Ok(i16::from_le_bytes(self.get_array()?))
120    }
121
122    /// # Errors
123    ///
124    /// Will return an error if not at least 3 bytes remain
125    #[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    /// # Errors
142    ///
143    /// Will return an error if not at least 3 bytes remain
144    #[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    /// # Errors
161    ///
162    /// Will return an error if less then the 4 required bytes for a `u32` remain
163    #[inline]
164    pub fn get_u32_le(&mut self) -> Result<u32, Error> {
165        Ok(u32::from_le_bytes(self.get_array()?))
166    }
167
168    /// # Errors
169    ///
170    /// Will return an error if less then the 4 required bytes for a `i32` remain
171    #[inline]
172    pub fn get_i32_le(&mut self) -> Result<i32, Error> {
173        Ok(i32::from_le_bytes(self.get_array()?))
174    }
175
176    /// # Errors
177    ///
178    /// Will return an error if less then the 8 required bytes for a `u64` remain
179    #[inline]
180    pub fn get_u64_le(&mut self) -> Result<u64, Error> {
181        Ok(u64::from_le_bytes(self.get_array()?))
182    }
183
184    /// # Errors
185    ///
186    /// Will return an error if less then the 8 required bytes for a `i64` remain
187    #[inline]
188    pub fn get_i64_le(&mut self) -> Result<i64, Error> {
189        Ok(i64::from_le_bytes(self.get_array()?))
190    }
191
192    /// # Errors
193    ///
194    /// Will return an error if less then the 4 required bytes for a `f32` remain
195    #[inline]
196    pub fn get_f32_le(&mut self) -> Result<f32, Error> {
197        Ok(f32::from_le_bytes(self.get_array()?))
198    }
199
200    /// # Errors
201    ///
202    /// Will return an error if less then the 8 required bytes for a `f64` remain
203    #[inline]
204    pub fn get_f64_le(&mut self) -> Result<f64, Error> {
205        Ok(f64::from_le_bytes(self.get_array()?))
206    }
207}