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
84
85
use core::marker::PhantomData;
pub mod header;
pub use header::*;
pub mod body;
pub use body::*;
pub trait Parse {
    
    type Output;
    
    type Error;
    
    fn parse(buff: &[u8]) -> Result<(Self::Output, usize), Self::Error>;
    
    fn parse_iter<'a>(buff: &'a [u8]) -> ParseIter<'a, Self::Output, Self::Error> {
        ParseIter {
            buff,
            index: 0,
            _t: PhantomData,
            _e: PhantomData,
        }
    }
}
pub struct ParseIter<'a, T, E> {
    buff: &'a [u8],
    index: usize,
    _t: PhantomData<T>,
    _e: PhantomData<E>,
}
impl<'a, T, E> Iterator for ParseIter<'a, T, E>
where
    T: Parse<Output = T, Error = E>,
{
    type Item = Result<T, E>;
    fn next(&mut self) -> Option<Self::Item> {
        if self.index == self.buff.len() {
            return None;
        }
        let (v, n) = match T::parse(&self.buff[self.index..]) {
            Ok((v, n)) => (v, n),
            Err(e) => return Some(Err(e)),
        };
        self.index += n;
        Some(Ok(v))
    }
}
pub trait Encode {
    
    type Error;
    
    fn encode(&self, buff: &mut [u8]) -> Result<usize, Self::Error>;
    
    fn encode_iter<'a, V: Iterator<Item = &'a Self>>(
        vals: V,
        buff: &mut [u8],
    ) -> Result<usize, Self::Error>
    where
        Self: 'static,
    {
        let mut index = 0;
        for i in vals {
            index += i.encode(&mut buff[index..])?;
        }
        Ok(index)
    }
}