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
use std::io::{self, Read, Write, BufReader, BufRead};
use std::ffi::{CString, CStr};
use ::{PodType, PodExt};
use uninitialized::{uninitialized, UNINITIALIZED};
use nue_io::ReadExact;
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) }
}
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) }
}
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: PodType> Encode for T {
type Options = ();
fn encode<W: Write>(&self, w: &mut W) -> io::Result<()> {
w.write_all(self.as_slice())
}
}
impl<T: PodType> Decode for T {
type Options = ();
fn decode<R: Read>(r: &mut R) -> io::Result<Self> {
let mut pod: Self = unsafe { uninitialized() };
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> {
use std::iter::repeat;
if let Some(len) = options.len {
let mut vec = Vec::with_capacity(len);
if UNINITIALIZED {
unsafe { vec.set_len(len); }
} else {
vec.extend(repeat(0).take(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 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<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_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(())
}
}
#[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,
}