| 1 | use crate::Error; |
| 2 | |
| 3 | type Result<T> = std::result::Result<T, Error>; |
| 4 | |
| 5 | pub(crate) struct Cursor<'a> { |
| 6 | pub(crate) bytes: &'a [u8], |
| 7 | pub(crate) offset: usize, |
| 8 | } |
| 9 | |
| 10 | impl<'a> Cursor<'a> { |
| 11 | pub(crate) fn take(&mut self, len: usize) -> Result<&'a [u8]> { |
| 12 | let Some((head, tail)) = self.bytes.split_at_checked(len) else { |
| 13 | return Err(Error { |
| 14 | offset: self.offset, |
| 15 | message: "Truncated structure", |
| 16 | }); |
| 17 | }; |
| 18 | self.bytes = tail; |
| 19 | self.offset += len; |
| 20 | Ok(head) |
| 21 | } |
| 22 | |
| 23 | pub(crate) fn read<const N: usize>(&mut self) -> Result<[u8; N]> { |
| 24 | let mut value = [0; N]; |
| 25 | value.copy_from_slice(self.take(N)?); |
| 26 | Ok(value) |
| 27 | } |
| 28 | } |