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
use core::fmt::Debug;
use crate::Error;
use super::Decode;
pub trait DecodePrefixed<'a, P: Decode<'a>> {
type Output: Debug;
type Error: Debug;
fn decode_prefixed(buff: &'a [u8]) -> Result<(Self::Output, usize), Self::Error>;
}
impl <'a, T, P> DecodePrefixed<'a, P> for T
where
T: Decode<'a>,
P: Decode<'a, Error=Error>,
<P as Decode<'a>>::Output: num_traits::AsPrimitive<usize>,
<T as Decode<'a>>::Error: From<Error>,
{
type Output = <T as Decode<'a>>::Output;
type Error = <T as Decode<'a>>::Error;
fn decode_prefixed(buff: &'a [u8]) -> Result<(Self::Output, usize), Self::Error> {
use num_traits::AsPrimitive;
let mut index = 0;
let (len, n) = P::decode(&buff)?;
index += n;
let (b, n) = T::decode(&buff[index..][..len.as_()])?;
index += n;
Ok((b, index))
}
}