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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use std::ffi;

use crate::*;

impl Decode for u8 {
    fn decode(buf: &mut Bytes) -> Result<Self> {
        if !buf.has_remaining() {
            return Err(Error::OutOfBounds);
        }

        Ok(buf.get_u8())
    }
}

impl Decode for i8 {
    fn decode(buf: &mut Bytes) -> Result<Self> {
        if !buf.has_remaining() {
            return Err(Error::OutOfBounds);
        }

        Ok(buf.get_i8())
    }
}

impl Decode for u16 {
    fn decode(buf: &mut Bytes) -> Result<Self> {
        Ok(Self::from_be_bytes(buf.decode()?))
    }
}

impl Decode for i16 {
    fn decode(buf: &mut Bytes) -> Result<Self> {
        Ok(Self::from_be_bytes(buf.decode()?))
    }
}

impl Decode for u32 {
    fn decode(buf: &mut Bytes) -> Result<Self> {
        Ok(Self::from_be_bytes(buf.decode()?))
    }
}

impl Decode for i32 {
    fn decode(buf: &mut Bytes) -> Result<Self> {
        Ok(Self::from_be_bytes(buf.decode()?))
    }
}

impl Decode for u64 {
    fn decode(buf: &mut Bytes) -> Result<Self> {
        Ok(Self::from_be_bytes(buf.decode()?))
    }
}

impl Decode for i64 {
    fn decode(buf: &mut Bytes) -> Result<Self> {
        Ok(Self::from_be_bytes(buf.decode()?))
    }
}

impl<const N: usize> Decode for [u8; N] {
    fn decode(buf: &mut Bytes) -> Result<Self> {
        if buf.remaining() < N {
            return Err(Error::OutOfBounds);
        }

        let mut bytes = [0; N];
        buf.copy_to_slice(&mut bytes);
        Ok(bytes)
    }
}

impl<T: Decode> Decode for Vec<T> {
    fn decode(buf: &mut Bytes) -> Result<Self> {
        let mut vec = Vec::new();
        while buf.has_remaining() {
            let item = buf.decode()?;
            vec.push(item);
        }

        Ok(vec)
    }
}

impl Decode for String {
    fn decode(buf: &mut Bytes) -> Result<Self> {
        let mut bytes = Vec::new();
        loop {
            let byte = buf.decode()?;
            if byte == 0 {
                break;
            }

            bytes.push(byte);
        }

        let str = ffi::CString::new(bytes).map_err(|err| Error::InvalidString(err.to_string()))?;
        str.into_string()
            .map_err(|err| Error::InvalidString(err.to_string()))
    }
}

impl<T: Decode> Decode for Option<T> {
    fn decode(buf: &mut Bytes) -> Result<Self> {
        if buf.has_remaining() {
            Ok(Some(buf.decode()?))
        } else {
            Ok(None)
        }
    }
}

impl Decode for Bytes {
    fn decode(buf: &mut Bytes) -> Result<Self> {
        Ok(buf.copy_to_bytes(buf.remaining()))
    }
}

impl Encode for u8 {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u8(*self);
        Ok(())
    }
}

impl Encode for i8 {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_i8(*self);
        Ok(())
    }
}

impl Encode for i16 {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_i16(*self);
        Ok(())
    }
}

impl Encode for u16 {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u16(*self);
        Ok(())
    }
}

impl Encode for u32 {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u32(*self);
        Ok(())
    }
}

impl Encode for i32 {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_i32(*self);
        Ok(())
    }
}

impl Encode for u64 {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u64(*self);
        Ok(())
    }
}

impl Encode for i64 {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_i64(*self);
        Ok(())
    }
}

impl<const N: usize> Encode for [u8; N] {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_slice(self);
        Ok(())
    }
}

impl<T: Encode> Encode for &[T] {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        for item in self.iter() {
            item.encode(buf)?;
        }

        Ok(())
    }
}

impl<T: Encode> Encode for Option<T> {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        match self {
            Some(v) => v.encode(buf),
            None => Ok(()),
        }
    }
}

impl Encode for &str {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_slice(self.as_bytes());
        buf.put_u8(0);
        Ok(())
    }
}

impl<T: Encode> Encode for Vec<T> {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        for item in self.iter() {
            item.encode(buf)?;
        }

        Ok(())
    }
}

impl Encode for Bytes {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_slice(self);
        Ok(())
    }
}

impl DecodeFrom for Bytes {
    fn decode<T: Decode>(&mut self) -> Result<T> {
        T::decode(self)
    }

    fn decode_exact<T: Decode>(&mut self, size: usize) -> Result<T> {
        T::decode_exact(self, size)
    }
}

impl EncodeTo for BytesMut {
    fn encode<T: Encode>(&mut self, v: &T) -> Result<()> {
        v.encode(self)
    }
}