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
use crate::*;
macro_rules! impl_encoder_fn {
    [$($data:tt)*] => (
        #[inline] fn encoder(&self, c: &mut impl Write) -> io::Result<()> {
            encode_len!(self $($data)*, c);
            c.write_all(self $($data)* .as_ref())
        }
    );
}
macro_rules! impl_encoder_for {
    [$($ty:ty; $rec_ty:ty),*] => {$(
        impl Encoder for $ty { impl_encoder_fn!(); }
        impl<Len: LenType> Encoder for $rec_ty { impl_encoder_fn!(.data); }
    )*};
    [$($data:tt)*] => (
        #[inline] fn encoder(&self, c: &mut impl Write) -> io::Result<()> {
            encode_len!(self $($data)*, c);
            c.write_all(self $($data)* .as_ref())
        }
    );
}
impl_encoder_for!(str; Record<Len, &str>, String; Record<Len, String>);

macro_rules! read_slice {
    [$c: expr] => ({
        let len = decode_len!($c);
        get_slice($c, len)
    });
}

impl<'de> Decoder<'de> for String {
    fn decoder(c: &mut &'de [u8]) -> Result<Self> {
        let data = read_slice!(c)?;
        String::from_utf8(data.to_vec()).map_err(DynErr::from)
    }
}

impl<'de, Len: LenType> Decoder<'de> for Record<Len, String> {
    fn decoder(c: &mut &'de [u8]) -> Result<Self> {
        let data = read_slice!(c)?;
        String::from_utf8(data.to_vec())
            .map_err(DynErr::from)
            .map(Record::new)
    }
}

impl<'de: 'a, 'a> Decoder<'de> for &'a str {
    fn decoder(c: &mut &'de [u8]) -> Result<Self> {
        let data = read_slice!(c)?;
        std::str::from_utf8(data).map_err(DynErr::from)
    }
}

impl<'de: 'a, 'a, Len: LenType> Decoder<'de> for Record<Len, &'a str> {
    fn decoder(c: &mut &'de [u8]) -> Result<Self> {
        let data = read_slice!(c)?;
        core::str::from_utf8(data)
            .map_err(DynErr::from)
            .map(Record::new)
    }
}

//------------------------------------------------------------------------------------------

impl<'de: 'a, 'a> Decoder<'de> for &'a [u8] {
    fn decoder(c: &mut &'de [u8]) -> Result<Self> {
        read_slice!(c)
    }
}

impl<'de: 'a, 'a, Len: LenType> Decoder<'de> for Record<Len, &'a [u8]> {
    fn decoder(c: &mut &'de [u8]) -> Result<Self> {
        read_slice!(c).map(Record::new)
    }
}