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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use core::fmt::Debug;
use crate::Error;
use super::Decode;
pub trait DecodeOwned {
    type Output: Debug;
    type Error: From<Error> + Debug;
    fn decode_owned(buff: &[u8]) -> Result<(Self::Output, usize), Self::Error>;
}
impl <'a, T: DecodeOwned> Decode<'a> for T {
    type Output = <T as DecodeOwned>::Output;
    type Error = <T as DecodeOwned>::Error;
    fn decode(buff: &'a [u8]) -> Result<(Self::Output, usize), Self::Error> {
        <T as DecodeOwned>::decode_owned(buff)
    }
}
#[cfg(feature = "alloc")]
impl <T> DecodeOwned for alloc::vec::Vec<T> 
where
    T: DecodeOwned<Output=T> + Debug,
    <T as DecodeOwned>::Error: From<Error> + Debug,
{
    type Error = <T as DecodeOwned>::Error;
    type Output = alloc::vec::Vec<<T as DecodeOwned>::Output>;
    fn decode_owned(buff: &[u8]) -> Result<(Self::Output, usize), Self::Error> {
        let mut index = 0;
        let mut v = alloc::vec::Vec::new();
        while index < buff.len() {
            let (d, n) = T::decode(&buff[index..])?;
            v.push(d);
            index += n;
        }
        Ok((v, index))
    }
}
#[cfg(feature = "heapless")]
impl <T, const N: usize> DecodeOwned for heapless::Vec<T, N> 
where
    T: DecodeOwned<Output=T> + Debug,
    <T as DecodeOwned>::Error: From<Error> + Debug,
{
    type Error = <T as DecodeOwned>::Error;
    type Output = heapless::Vec<<T as DecodeOwned>::Output, N>;
    fn decode_owned(buff: &[u8]) -> Result<(Self::Output, usize), Self::Error> {
        let mut index = 0;
        let mut v = heapless::Vec::new();
        while index < buff.len() {
            let (d, n) = T::decode(&buff[index..])?;
            if let Err(_e) = v.push(d) {
                return Err(Error::BufferOverrun.into())
            }
            index += n;
        }
        Ok((v, index))
    }
}