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
use crate::{DecodeError, DecodeErrorHandler, NestedDecode, NestedDecodeInput};
impl<'a> NestedDecodeInput for &'a [u8] {
fn remaining_len(&self) -> usize {
self.len()
}
fn peek_into<H>(&mut self, into: &mut [u8], h: H) -> Result<(), H::HandledErr>
where
H: DecodeErrorHandler,
{
if into.len() > self.len() {
return Err(h.handle_error(DecodeError::INPUT_TOO_SHORT));
}
let len = into.len();
into.copy_from_slice(&self[..len]);
Ok(())
}
fn read_into<H>(&mut self, into: &mut [u8], h: H) -> Result<(), H::HandledErr>
where
H: DecodeErrorHandler,
{
self.peek_into(into, h)?;
*self = &self[into.len()..];
Ok(())
}
}
pub fn dep_decode_from_byte_slice<T, H>(input: &[u8], h: H) -> Result<T, H::HandledErr>
where
T: NestedDecode,
H: DecodeErrorHandler,
{
let mut_slice = &mut &*input;
let result = T::dep_decode_or_handle_err(mut_slice, h)?;
if !mut_slice.is_empty() {
return Err(h.handle_error(DecodeError::INPUT_TOO_LONG));
}
Ok(result)
}