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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use std::io::{self, Read, Write, BufReader, BufRead, Cursor};
use std::ffi::{CString, CStr};
use ::Pod;
use uninitialized::UNINITIALIZED;
use nue_io::ReadExactExt;
pub trait Encode {
type Options: Default;
fn encode<W: Write>(&self, w: &mut W) -> io::Result<()> { self.encode_options(w, Default::default()) }
fn encode_options<W: Write>(&self, w: &mut W, _options: Self::Options) -> io::Result<()> { self.encode(w) }
fn encode_vec(&self) -> io::Result<Vec<u8>> {
let data = Vec::new();
let mut cursor = Cursor::new(data);
try!(self.encode(&mut cursor));
Ok(cursor.into_inner())
}
fn encode_vec_options(&self, options: Self::Options) -> io::Result<Vec<u8>> {
let data = Vec::new();
let mut cursor = Cursor::new(data);
try!(self.encode_options(&mut cursor, options));
Ok(cursor.into_inner())
}
}
pub trait Decode: Sized {
type Options: Default;
fn decode<R: Read>(r: &mut R) -> io::Result<Self> { Self::decode_options(r, Default::default()) }
fn decode_options<R: Read>(r: &mut R, _options: Self::Options) -> io::Result<Self> { Self::decode(r) }
fn decode_slice(data: &[u8]) -> io::Result<Self> {
let mut cursor = Cursor::new(data);
Self::decode(&mut cursor)
}
fn decode_slice_options(data: &[u8], options: Self::Options) -> io::Result<Self> {
let mut cursor = Cursor::new(data);
Self::decode_options(&mut cursor, options)
}
fn validate(&self) -> io::Result<()> { Ok(()) }
}
impl<T: Encode> Encode for Option<T> {
type Options = T::Options;
fn encode<W: Write>(&self, w: &mut W) -> io::Result<()> {
self.as_ref().map(|v| v.encode(w)).unwrap_or(Ok(()))
}
fn encode_options<W: Write>(&self, w: &mut W, options: Self::Options) -> io::Result<()> {
self.as_ref().map(|v| v.encode_options(w, options)).unwrap_or(Ok(()))
}
}
impl<T: Decode> Decode for Option<T> {
type Options = T::Options;
fn decode<R: Read>(r: &mut R) -> io::Result<Self> {
T::decode(r).map(Some)
}
fn decode_options<R: Read>(r: &mut R, options: Self::Options) -> io::Result<Self> {
T::decode_options(r, options).map(Some)
}
}
impl<T: Pod> Encode for T {
type Options = ();
fn encode<W: Write>(&self, w: &mut W) -> io::Result<()> {
w.write_all(self.as_slice())
}
}
impl<T: Pod> Decode for T {
type Options = ();
fn decode<R: Read>(r: &mut R) -> io::Result<Self> {
let mut pod: Self = Pod::zeroed();
try!(r.read_exact(pod.mut_slice()));
Ok(pod)
}
}
impl Decode for String {
type Options = StringDecodeOptions;
fn decode_options<R: Read>(r: &mut R, options: Self::Options) -> io::Result<Self> {
if let Some(len) = options.len {
let mut vec = if UNINITIALIZED {
let mut vec = Vec::with_capacity(len);
unsafe { vec.set_len(len); }
vec
} else {
vec![0; len]
};
try!(r.read_exact(&mut vec[..]));
String::from_utf8(vec).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))
} else {
let mut string = String::new();
try!(r.read_to_string(&mut string));
Ok(string)
}
}
}
impl Encode for String {
type Options = ();
fn encode<W: Write>(&self, w: &mut W) -> io::Result<()> {
(**self).encode(w)
}
}
impl Encode for str {
type Options = ();
fn encode<W: Write>(&self, w: &mut W) -> io::Result<()> {
w.write_all(self.as_bytes())
}
}
impl<'a> Encode for &'a str {
type Options = ();
fn encode<W: Write>(&self, w: &mut W) -> io::Result<()> {
(*self).encode(w)
}
}
impl Decode for CString {
type Options = CStringDecodeOptions;
fn decode_options<R: Read>(r: &mut R, options: Self::Options) -> io::Result<Self> {
if options.require_nul {
unimplemented!()
}
unsafe {
let mut err = Ok(());
let string = CString::from_vec_unchecked(r.bytes().map(|c| match c {
Ok(c) => c,
Err(e) => {
err = Err(e);
0
},
}).take_while(|&c| c > 0).collect());
err.map(|_| string)
}
}
}
impl Encode for CString {
type Options = ();
fn encode<W: Write>(&self, w: &mut W) -> io::Result<()> {
(**self).encode(w)
}
}
impl Encode for CStr {
type Options = ();
fn encode<W: Write>(&self, w: &mut W) -> io::Result<()> {
w.write_all(self.to_bytes_with_nul())
}
}
impl<'a> Encode for &'a CStr {
type Options = ();
fn encode<W: Write>(&self, w: &mut W) -> io::Result<()> {
(*self).encode(w)
}
}
impl<T: Decode> Decode for Vec<T> where T::Options: Clone {
type Options = VecDecodeOptions<T::Options>;
fn decode_options<R: Read>(r: &mut R, options: Self::Options) -> io::Result<Self> {
let mut vec = Vec::with_capacity(options.len.unwrap_or(0));
if let Some(len) = options.len {
for _ in 0..len {
vec.push(try!(T::decode_options(r, options.options.clone())));
}
} else {
let r = &mut BufReader::new(r);
while try!(r.fill_buf()).len() > 0 {
vec.push(try!(T::decode_options(r, options.options.clone())));
}
}
Ok(vec)
}
}
impl<T: Encode> Encode for Vec<T> where T::Options: Clone {
type Options = T::Options;
fn encode<W: Write>(&self, w: &mut W) -> io::Result<()> {
(**self).encode(w)
}
fn encode_options<W: Write>(&self, w: &mut W, options: Self::Options) -> io::Result<()> {
(**self).encode_options(w, options)
}
}
impl<T: Encode> Encode for [T] where T::Options: Clone {
type Options = T::Options;
fn encode<W: Write>(&self, w: &mut W) -> io::Result<()> {
for ref v in self {
try!(v.encode(w));
}
Ok(())
}
fn encode_options<W: Write>(&self, w: &mut W, options: Self::Options) -> io::Result<()> {
for ref v in self {
try!(v.encode_options(w, options.clone()));
}
Ok(())
}
}
impl<'a, T: Encode> Encode for &'a [T] where T::Options: Clone {
type Options = T::Options;
fn encode<W: Write>(&self, w: &mut W) -> io::Result<()> {
(*self).encode(w)
}
fn encode_options<W: Write>(&self, w: &mut W, options: Self::Options) -> io::Result<()> {
(*self).encode_options(w, options)
}
}
#[derive(Clone, Default, Debug)]
pub struct VecDecodeOptions<T> {
pub len: Option<usize>,
pub options: T,
}
#[derive(Copy, Clone, Default, Debug)]
pub struct StringDecodeOptions {
pub len: Option<usize>,
}
#[derive(Copy, Clone, Default, Debug)]
pub struct CStringDecodeOptions {
pub require_nul: bool,
}