]>
Commit | Line | Data |
---|---|---|
9cc50fc6 SL |
1 | // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | use Error as DecodeError; | |
12 | use writer::EncodeResult; | |
13 | use leb128::{read_signed_leb128, read_unsigned_leb128, write_signed_leb128, write_unsigned_leb128}; | |
14 | use std::io::{self, Write}; | |
15 | use serialize; | |
16 | ||
17 | // ----------------------------------------------------------------------------- | |
18 | // Encoder | |
19 | // ----------------------------------------------------------------------------- | |
20 | ||
21 | pub struct Encoder<'a> { | |
22 | pub cursor: &'a mut io::Cursor<Vec<u8>>, | |
23 | } | |
24 | ||
25 | impl<'a> Encoder<'a> { | |
26 | pub fn new(cursor: &'a mut io::Cursor<Vec<u8>>) -> Encoder<'a> { | |
27 | Encoder { cursor: cursor } | |
28 | } | |
29 | } | |
30 | ||
31 | ||
32 | macro_rules! write_uleb128 { | |
33 | ($enc:expr, $value:expr) => {{ | |
34 | let pos = $enc.cursor.position() as usize; | |
35 | let bytes_written = write_unsigned_leb128($enc.cursor.get_mut(), pos, $value as u64); | |
36 | $enc.cursor.set_position((pos + bytes_written) as u64); | |
37 | Ok(()) | |
38 | }} | |
39 | } | |
40 | ||
41 | macro_rules! write_sleb128 { | |
42 | ($enc:expr, $value:expr) => {{ | |
43 | let pos = $enc.cursor.position() as usize; | |
44 | let bytes_written = write_signed_leb128($enc.cursor.get_mut(), pos, $value as i64); | |
45 | $enc.cursor.set_position((pos + bytes_written) as u64); | |
46 | Ok(()) | |
47 | }} | |
48 | } | |
49 | ||
50 | impl<'a> serialize::Encoder for Encoder<'a> { | |
51 | type Error = io::Error; | |
52 | ||
53 | fn emit_nil(&mut self) -> EncodeResult { | |
54 | Ok(()) | |
55 | } | |
56 | ||
57 | fn emit_uint(&mut self, v: usize) -> EncodeResult { | |
58 | write_uleb128!(self, v) | |
59 | } | |
60 | ||
61 | fn emit_u64(&mut self, v: u64) -> EncodeResult { | |
62 | write_uleb128!(self, v) | |
63 | } | |
64 | ||
65 | fn emit_u32(&mut self, v: u32) -> EncodeResult { | |
66 | write_uleb128!(self, v) | |
67 | } | |
68 | ||
69 | fn emit_u16(&mut self, v: u16) -> EncodeResult { | |
70 | write_uleb128!(self, v) | |
71 | } | |
72 | ||
73 | fn emit_u8(&mut self, v: u8) -> EncodeResult { | |
74 | let _ = self.cursor.write_all(&[v]); | |
75 | Ok(()) | |
76 | } | |
77 | ||
78 | fn emit_int(&mut self, v: isize) -> EncodeResult { | |
79 | write_sleb128!(self, v) | |
80 | } | |
81 | ||
82 | fn emit_i64(&mut self, v: i64) -> EncodeResult { | |
83 | write_sleb128!(self, v) | |
84 | } | |
85 | ||
86 | fn emit_i32(&mut self, v: i32) -> EncodeResult { | |
87 | write_sleb128!(self, v) | |
88 | } | |
89 | ||
90 | fn emit_i16(&mut self, v: i16) -> EncodeResult { | |
91 | write_sleb128!(self, v) | |
92 | } | |
93 | ||
94 | fn emit_i8(&mut self, v: i8) -> EncodeResult { | |
95 | let as_u8: u8 = unsafe { ::std::mem::transmute(v) }; | |
96 | let _ = self.cursor.write_all(&[as_u8]); | |
97 | Ok(()) | |
98 | } | |
99 | ||
100 | fn emit_bool(&mut self, v: bool) -> EncodeResult { | |
101 | self.emit_u8(if v { | |
102 | 1 | |
103 | } else { | |
104 | 0 | |
105 | }) | |
106 | } | |
107 | ||
108 | fn emit_f64(&mut self, v: f64) -> EncodeResult { | |
109 | let as_u64: u64 = unsafe { ::std::mem::transmute(v) }; | |
110 | self.emit_u64(as_u64) | |
111 | } | |
112 | ||
113 | fn emit_f32(&mut self, v: f32) -> EncodeResult { | |
114 | let as_u32: u32 = unsafe { ::std::mem::transmute(v) }; | |
115 | self.emit_u32(as_u32) | |
116 | } | |
117 | ||
118 | fn emit_char(&mut self, v: char) -> EncodeResult { | |
119 | self.emit_u32(v as u32) | |
120 | } | |
121 | ||
122 | fn emit_str(&mut self, v: &str) -> EncodeResult { | |
54a0048b | 123 | self.emit_uint(v.len())?; |
9cc50fc6 SL |
124 | let _ = self.cursor.write_all(v.as_bytes()); |
125 | Ok(()) | |
126 | } | |
127 | ||
128 | fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult | |
129 | where F: FnOnce(&mut Self) -> EncodeResult | |
130 | { | |
131 | f(self) | |
132 | } | |
133 | ||
134 | fn emit_enum_variant<F>(&mut self, | |
135 | _v_name: &str, | |
136 | v_id: usize, | |
137 | _len: usize, | |
138 | f: F) | |
139 | -> EncodeResult | |
140 | where F: FnOnce(&mut Self) -> EncodeResult | |
141 | { | |
54a0048b | 142 | self.emit_uint(v_id)?; |
9cc50fc6 SL |
143 | f(self) |
144 | } | |
145 | ||
146 | fn emit_enum_variant_arg<F>(&mut self, _: usize, f: F) -> EncodeResult | |
147 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
148 | { | |
149 | f(self) | |
150 | } | |
151 | ||
152 | fn emit_enum_struct_variant<F>(&mut self, | |
153 | v_name: &str, | |
154 | v_id: usize, | |
155 | cnt: usize, | |
156 | f: F) | |
157 | -> EncodeResult | |
158 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
159 | { | |
160 | self.emit_enum_variant(v_name, v_id, cnt, f) | |
161 | } | |
162 | ||
163 | fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult | |
164 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
165 | { | |
166 | self.emit_enum_variant_arg(idx, f) | |
167 | } | |
168 | ||
169 | fn emit_struct<F>(&mut self, _: &str, _len: usize, f: F) -> EncodeResult | |
170 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
171 | { | |
172 | f(self) | |
173 | } | |
174 | ||
175 | fn emit_struct_field<F>(&mut self, _name: &str, _: usize, f: F) -> EncodeResult | |
176 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
177 | { | |
178 | f(self) | |
179 | } | |
180 | ||
181 | fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult | |
182 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
183 | { | |
184 | self.emit_seq(len, f) | |
185 | } | |
186 | ||
187 | fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult | |
188 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
189 | { | |
190 | self.emit_seq_elt(idx, f) | |
191 | } | |
192 | ||
193 | fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult | |
194 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
195 | { | |
196 | self.emit_seq(len, f) | |
197 | } | |
198 | ||
199 | fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult | |
200 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
201 | { | |
202 | self.emit_seq_elt(idx, f) | |
203 | } | |
204 | ||
205 | fn emit_option<F>(&mut self, f: F) -> EncodeResult | |
206 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
207 | { | |
208 | self.emit_enum("Option", f) | |
209 | } | |
210 | ||
211 | fn emit_option_none(&mut self) -> EncodeResult { | |
212 | self.emit_enum_variant("None", 0, 0, |_| Ok(())) | |
213 | } | |
214 | ||
215 | fn emit_option_some<F>(&mut self, f: F) -> EncodeResult | |
216 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
217 | { | |
218 | self.emit_enum_variant("Some", 1, 1, f) | |
219 | } | |
220 | ||
221 | fn emit_seq<F>(&mut self, len: usize, f: F) -> EncodeResult | |
222 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
223 | { | |
54a0048b | 224 | self.emit_uint(len)?; |
9cc50fc6 SL |
225 | f(self) |
226 | } | |
227 | ||
228 | fn emit_seq_elt<F>(&mut self, _idx: usize, f: F) -> EncodeResult | |
229 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
230 | { | |
231 | f(self) | |
232 | } | |
233 | ||
234 | fn emit_map<F>(&mut self, len: usize, f: F) -> EncodeResult | |
235 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
236 | { | |
54a0048b | 237 | self.emit_uint(len)?; |
9cc50fc6 SL |
238 | f(self) |
239 | } | |
240 | ||
241 | fn emit_map_elt_key<F>(&mut self, _idx: usize, f: F) -> EncodeResult | |
242 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
243 | { | |
244 | f(self) | |
245 | } | |
246 | ||
247 | fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult | |
248 | where F: FnOnce(&mut Encoder<'a>) -> EncodeResult | |
249 | { | |
250 | f(self) | |
251 | } | |
252 | } | |
253 | ||
254 | impl<'a> Encoder<'a> { | |
255 | pub fn position(&self) -> usize { | |
256 | self.cursor.position() as usize | |
257 | } | |
258 | ||
259 | pub fn from_rbml<'b: 'c, 'c>(rbml: &'c mut ::writer::Encoder<'b>) -> Encoder<'c> { | |
260 | Encoder { cursor: rbml.writer } | |
261 | } | |
262 | } | |
263 | ||
264 | // ----------------------------------------------------------------------------- | |
265 | // Decoder | |
266 | // ----------------------------------------------------------------------------- | |
267 | ||
268 | pub struct Decoder<'a> { | |
269 | pub data: &'a [u8], | |
270 | position: usize, | |
271 | } | |
272 | ||
273 | impl<'a> Decoder<'a> { | |
274 | pub fn new(data: &'a [u8], position: usize) -> Decoder<'a> { | |
275 | Decoder { | |
276 | data: data, | |
277 | position: position, | |
278 | } | |
279 | } | |
280 | ||
281 | pub fn position(&self) -> usize { | |
282 | self.position | |
283 | } | |
284 | ||
285 | pub fn advance(&mut self, bytes: usize) { | |
286 | self.position += bytes; | |
287 | } | |
288 | } | |
289 | ||
290 | macro_rules! read_uleb128 { | |
291 | ($dec:expr, $t:ty) => ({ | |
292 | let (value, bytes_read) = read_unsigned_leb128($dec.data, $dec.position); | |
293 | $dec.position += bytes_read; | |
294 | Ok(value as $t) | |
295 | }) | |
296 | } | |
297 | ||
298 | macro_rules! read_sleb128 { | |
299 | ($dec:expr, $t:ty) => ({ | |
300 | let (value, bytes_read) = read_signed_leb128($dec.data, $dec.position); | |
301 | $dec.position += bytes_read; | |
302 | Ok(value as $t) | |
303 | }) | |
304 | } | |
305 | ||
306 | ||
307 | impl<'a> serialize::Decoder for Decoder<'a> { | |
308 | type Error = DecodeError; | |
309 | ||
310 | fn read_nil(&mut self) -> Result<(), Self::Error> { | |
311 | Ok(()) | |
312 | } | |
313 | ||
314 | fn read_u64(&mut self) -> Result<u64, Self::Error> { | |
315 | read_uleb128!(self, u64) | |
316 | } | |
317 | ||
318 | fn read_u32(&mut self) -> Result<u32, Self::Error> { | |
319 | read_uleb128!(self, u32) | |
320 | } | |
321 | ||
322 | fn read_u16(&mut self) -> Result<u16, Self::Error> { | |
323 | read_uleb128!(self, u16) | |
324 | } | |
325 | ||
326 | fn read_u8(&mut self) -> Result<u8, Self::Error> { | |
327 | let value = self.data[self.position]; | |
328 | self.position += 1; | |
329 | Ok(value) | |
330 | } | |
331 | ||
332 | fn read_uint(&mut self) -> Result<usize, Self::Error> { | |
333 | read_uleb128!(self, usize) | |
334 | } | |
335 | ||
336 | fn read_i64(&mut self) -> Result<i64, Self::Error> { | |
337 | read_sleb128!(self, i64) | |
338 | } | |
339 | ||
340 | fn read_i32(&mut self) -> Result<i32, Self::Error> { | |
341 | read_sleb128!(self, i32) | |
342 | } | |
343 | ||
344 | fn read_i16(&mut self) -> Result<i16, Self::Error> { | |
345 | read_sleb128!(self, i16) | |
346 | } | |
347 | ||
348 | fn read_i8(&mut self) -> Result<i8, Self::Error> { | |
349 | let as_u8 = self.data[self.position]; | |
350 | self.position += 1; | |
351 | unsafe { Ok(::std::mem::transmute(as_u8)) } | |
352 | } | |
353 | ||
354 | fn read_int(&mut self) -> Result<isize, Self::Error> { | |
355 | read_sleb128!(self, isize) | |
356 | } | |
357 | ||
358 | fn read_bool(&mut self) -> Result<bool, Self::Error> { | |
54a0048b | 359 | let value = self.read_u8()?; |
9cc50fc6 SL |
360 | Ok(value != 0) |
361 | } | |
362 | ||
363 | fn read_f64(&mut self) -> Result<f64, Self::Error> { | |
54a0048b | 364 | let bits = self.read_u64()?; |
9cc50fc6 SL |
365 | Ok(unsafe { ::std::mem::transmute(bits) }) |
366 | } | |
367 | ||
368 | fn read_f32(&mut self) -> Result<f32, Self::Error> { | |
54a0048b | 369 | let bits = self.read_u32()?; |
9cc50fc6 SL |
370 | Ok(unsafe { ::std::mem::transmute(bits) }) |
371 | } | |
372 | ||
373 | fn read_char(&mut self) -> Result<char, Self::Error> { | |
54a0048b | 374 | let bits = self.read_u32()?; |
9cc50fc6 SL |
375 | Ok(::std::char::from_u32(bits).unwrap()) |
376 | } | |
377 | ||
378 | fn read_str(&mut self) -> Result<String, Self::Error> { | |
54a0048b | 379 | let len = self.read_uint()?; |
9cc50fc6 SL |
380 | let s = ::std::str::from_utf8(&self.data[self.position..self.position + len]).unwrap(); |
381 | self.position += len; | |
382 | Ok(s.to_string()) | |
383 | } | |
384 | ||
385 | fn read_enum<T, F>(&mut self, _name: &str, f: F) -> Result<T, Self::Error> | |
386 | where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error> | |
387 | { | |
388 | f(self) | |
389 | } | |
390 | ||
391 | fn read_enum_variant<T, F>(&mut self, _: &[&str], mut f: F) -> Result<T, Self::Error> | |
392 | where F: FnMut(&mut Decoder<'a>, usize) -> Result<T, Self::Error> | |
393 | { | |
54a0048b | 394 | let disr = self.read_uint()?; |
9cc50fc6 SL |
395 | f(self, disr) |
396 | } | |
397 | ||
398 | fn read_enum_variant_arg<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error> | |
399 | where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error> | |
400 | { | |
401 | f(self) | |
402 | } | |
403 | ||
404 | fn read_enum_struct_variant<T, F>(&mut self, _: &[&str], mut f: F) -> Result<T, Self::Error> | |
405 | where F: FnMut(&mut Decoder<'a>, usize) -> Result<T, Self::Error> | |
406 | { | |
54a0048b | 407 | let disr = self.read_uint()?; |
9cc50fc6 SL |
408 | f(self, disr) |
409 | } | |
410 | ||
411 | fn read_enum_struct_variant_field<T, F>(&mut self, | |
412 | _name: &str, | |
413 | _idx: usize, | |
414 | f: F) | |
415 | -> Result<T, Self::Error> | |
416 | where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error> | |
417 | { | |
418 | f(self) | |
419 | } | |
420 | ||
421 | fn read_struct<T, F>(&mut self, _name: &str, _: usize, f: F) -> Result<T, Self::Error> | |
422 | where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error> | |
423 | { | |
424 | f(self) | |
425 | } | |
426 | ||
427 | fn read_struct_field<T, F>(&mut self, _name: &str, _idx: usize, f: F) -> Result<T, Self::Error> | |
428 | where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error> | |
429 | { | |
430 | f(self) | |
431 | } | |
432 | ||
433 | fn read_tuple<T, F>(&mut self, tuple_len: usize, f: F) -> Result<T, Self::Error> | |
434 | where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error> | |
435 | { | |
436 | self.read_seq(move |d, len| { | |
437 | if len == tuple_len { | |
438 | f(d) | |
439 | } else { | |
440 | let err = format!("Invalid tuple length. Expected {}, found {}", | |
441 | tuple_len, | |
442 | len); | |
443 | Err(DecodeError::Expected(err)) | |
444 | } | |
445 | }) | |
446 | } | |
447 | ||
448 | fn read_tuple_arg<T, F>(&mut self, idx: usize, f: F) -> Result<T, Self::Error> | |
449 | where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error> | |
450 | { | |
451 | self.read_seq_elt(idx, f) | |
452 | } | |
453 | ||
454 | fn read_tuple_struct<T, F>(&mut self, _name: &str, len: usize, f: F) -> Result<T, Self::Error> | |
455 | where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error> | |
456 | { | |
457 | self.read_tuple(len, f) | |
458 | } | |
459 | ||
460 | fn read_tuple_struct_arg<T, F>(&mut self, idx: usize, f: F) -> Result<T, Self::Error> | |
461 | where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error> | |
462 | { | |
463 | self.read_tuple_arg(idx, f) | |
464 | } | |
465 | ||
466 | fn read_option<T, F>(&mut self, mut f: F) -> Result<T, Self::Error> | |
467 | where F: FnMut(&mut Decoder<'a>, bool) -> Result<T, Self::Error> | |
468 | { | |
469 | self.read_enum("Option", move |this| { | |
470 | this.read_enum_variant(&["None", "Some"], move |this, idx| { | |
471 | match idx { | |
472 | 0 => f(this, false), | |
473 | 1 => f(this, true), | |
474 | _ => { | |
475 | let msg = format!("Invalid Option index: {}", idx); | |
476 | Err(DecodeError::Expected(msg)) | |
477 | } | |
478 | } | |
479 | }) | |
480 | }) | |
481 | } | |
482 | ||
483 | fn read_seq<T, F>(&mut self, f: F) -> Result<T, Self::Error> | |
484 | where F: FnOnce(&mut Decoder<'a>, usize) -> Result<T, Self::Error> | |
485 | { | |
54a0048b | 486 | let len = self.read_uint()?; |
9cc50fc6 SL |
487 | f(self, len) |
488 | } | |
489 | ||
490 | fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error> | |
491 | where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error> | |
492 | { | |
493 | f(self) | |
494 | } | |
495 | ||
496 | fn read_map<T, F>(&mut self, f: F) -> Result<T, Self::Error> | |
497 | where F: FnOnce(&mut Decoder<'a>, usize) -> Result<T, Self::Error> | |
498 | { | |
54a0048b | 499 | let len = self.read_uint()?; |
9cc50fc6 SL |
500 | f(self, len) |
501 | } | |
502 | ||
503 | fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error> | |
504 | where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error> | |
505 | { | |
506 | f(self) | |
507 | } | |
508 | ||
509 | fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error> | |
510 | where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error> | |
511 | { | |
512 | f(self) | |
513 | } | |
514 | ||
515 | fn error(&mut self, err: &str) -> Self::Error { | |
516 | DecodeError::ApplicationError(err.to_string()) | |
517 | } | |
518 | } | |
519 | ||
520 | ||
521 | #[cfg(test)] | |
522 | mod tests { | |
523 | use serialize::{Encodable, Decodable}; | |
524 | use std::io::Cursor; | |
525 | use std::fmt::Debug; | |
526 | use super::{Encoder, Decoder}; | |
527 | ||
528 | #[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)] | |
529 | struct Struct { | |
530 | a: (), | |
531 | b: u8, | |
532 | c: u16, | |
533 | d: u32, | |
534 | e: u64, | |
535 | f: usize, | |
536 | ||
537 | g: i8, | |
538 | h: i16, | |
539 | i: i32, | |
540 | j: i64, | |
541 | k: isize, | |
542 | ||
543 | l: char, | |
544 | m: String, | |
545 | n: f32, | |
546 | o: f64, | |
547 | p: bool, | |
548 | q: Option<u32>, | |
549 | } | |
550 | ||
551 | ||
552 | fn check_round_trip<T: Encodable + Decodable + PartialEq + Debug>(values: Vec<T>) { | |
553 | let mut cursor = Cursor::new(Vec::new()); | |
554 | ||
555 | for value in &values { | |
556 | let mut encoder = Encoder::new(&mut cursor); | |
557 | Encodable::encode(&value, &mut encoder).unwrap(); | |
558 | } | |
559 | ||
560 | let data = cursor.into_inner(); | |
561 | let mut decoder = Decoder::new(&data[..], 0); | |
562 | ||
563 | for value in values { | |
564 | let decoded = Decodable::decode(&mut decoder).unwrap(); | |
565 | assert_eq!(value, decoded); | |
566 | } | |
567 | } | |
568 | ||
569 | #[test] | |
570 | fn test_unit() { | |
571 | check_round_trip(vec![(), (), (), ()]); | |
572 | } | |
573 | ||
574 | #[test] | |
575 | fn test_u8() { | |
576 | let mut vec = vec![]; | |
577 | for i in ::std::u8::MIN..::std::u8::MAX { | |
578 | vec.push(i); | |
579 | } | |
580 | check_round_trip(vec); | |
581 | } | |
582 | ||
583 | #[test] | |
584 | fn test_u16() { | |
585 | for i in ::std::u16::MIN..::std::u16::MAX { | |
586 | check_round_trip(vec![1, 2, 3, i, i, i]); | |
587 | } | |
588 | } | |
589 | ||
590 | #[test] | |
591 | fn test_u32() { | |
592 | check_round_trip(vec![1, 2, 3, ::std::u32::MIN, 0, 1, ::std::u32::MAX, 2, 1]); | |
593 | } | |
594 | ||
595 | #[test] | |
596 | fn test_u64() { | |
597 | check_round_trip(vec![1, 2, 3, ::std::u64::MIN, 0, 1, ::std::u64::MAX, 2, 1]); | |
598 | } | |
599 | ||
600 | #[test] | |
601 | fn test_usize() { | |
602 | check_round_trip(vec![1, 2, 3, ::std::usize::MIN, 0, 1, ::std::usize::MAX, 2, 1]); | |
603 | } | |
604 | ||
605 | #[test] | |
606 | fn test_i8() { | |
607 | let mut vec = vec![]; | |
608 | for i in ::std::i8::MIN..::std::i8::MAX { | |
609 | vec.push(i); | |
610 | } | |
611 | check_round_trip(vec); | |
612 | } | |
613 | ||
614 | #[test] | |
615 | fn test_i16() { | |
616 | for i in ::std::i16::MIN..::std::i16::MAX { | |
617 | check_round_trip(vec![-1, 2, -3, i, i, i, 2]); | |
618 | } | |
619 | } | |
620 | ||
621 | #[test] | |
622 | fn test_i32() { | |
623 | check_round_trip(vec![-1, 2, -3, ::std::i32::MIN, 0, 1, ::std::i32::MAX, 2, 1]); | |
624 | } | |
625 | ||
626 | #[test] | |
627 | fn test_i64() { | |
628 | check_round_trip(vec![-1, 2, -3, ::std::i64::MIN, 0, 1, ::std::i64::MAX, 2, 1]); | |
629 | } | |
630 | ||
631 | #[test] | |
632 | fn test_isize() { | |
633 | check_round_trip(vec![-1, 2, -3, ::std::isize::MIN, 0, 1, ::std::isize::MAX, 2, 1]); | |
634 | } | |
635 | ||
636 | #[test] | |
637 | fn test_bool() { | |
638 | check_round_trip(vec![false, true, true, false, false]); | |
639 | } | |
640 | ||
641 | #[test] | |
642 | fn test_f32() { | |
643 | let mut vec = vec![]; | |
644 | for i in -100..100 { | |
645 | vec.push((i as f32) / 3.0); | |
646 | } | |
647 | check_round_trip(vec); | |
648 | } | |
649 | ||
650 | #[test] | |
651 | fn test_f64() { | |
652 | let mut vec = vec![]; | |
653 | for i in -100..100 { | |
654 | vec.push((i as f64) / 3.0); | |
655 | } | |
656 | check_round_trip(vec); | |
657 | } | |
658 | ||
659 | #[test] | |
660 | fn test_char() { | |
661 | let vec = vec!['a', 'b', 'c', 'd', 'A', 'X', ' ', '#', 'Ö', 'Ä', 'µ', '€']; | |
662 | check_round_trip(vec); | |
663 | } | |
664 | ||
665 | #[test] | |
666 | fn test_string() { | |
667 | let vec = vec!["abcbuÖeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(), | |
668 | "abcbuÖganeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(), | |
669 | "abcbuÖganeiovÄnameÜavmpßvmea€µsbpapmaebn".to_string(), | |
670 | "abcbuÖganeiovÄnameÜavmpßvmeabpnvapeapmaebn".to_string(), | |
671 | "abcbuÖganeiÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(), | |
672 | "abcbuÖganeiovÄnameÜavmpßvmea€µsbpmaebn".to_string(), | |
673 | "abcbuÖganeiovÄnameÜavmpßvmea€µnvapeapmaebn".to_string()]; | |
674 | ||
675 | check_round_trip(vec); | |
676 | } | |
677 | ||
678 | #[test] | |
679 | fn test_option() { | |
680 | check_round_trip(vec![Some(-1i8)]); | |
681 | check_round_trip(vec![Some(-2i16)]); | |
682 | check_round_trip(vec![Some(-3i32)]); | |
683 | check_round_trip(vec![Some(-4i64)]); | |
684 | check_round_trip(vec![Some(-5isize)]); | |
685 | ||
686 | let none_i8: Option<i8> = None; | |
687 | check_round_trip(vec![none_i8]); | |
688 | ||
689 | let none_i16: Option<i16> = None; | |
690 | check_round_trip(vec![none_i16]); | |
691 | ||
692 | let none_i32: Option<i32> = None; | |
693 | check_round_trip(vec![none_i32]); | |
694 | ||
695 | let none_i64: Option<i64> = None; | |
696 | check_round_trip(vec![none_i64]); | |
697 | ||
698 | let none_isize: Option<isize> = None; | |
699 | check_round_trip(vec![none_isize]); | |
700 | } | |
701 | ||
702 | #[test] | |
703 | fn test_struct() { | |
704 | check_round_trip(vec![Struct { | |
705 | a: (), | |
706 | b: 10, | |
707 | c: 11, | |
708 | d: 12, | |
709 | e: 13, | |
710 | f: 14, | |
711 | ||
712 | g: 15, | |
713 | h: 16, | |
714 | i: 17, | |
715 | j: 18, | |
716 | k: 19, | |
717 | ||
718 | l: 'x', | |
719 | m: "abc".to_string(), | |
720 | n: 20.5, | |
721 | o: 21.5, | |
722 | p: false, | |
723 | q: None, | |
724 | }]); | |
725 | ||
726 | check_round_trip(vec![Struct { | |
727 | a: (), | |
728 | b: 101, | |
729 | c: 111, | |
730 | d: 121, | |
731 | e: 131, | |
732 | f: 141, | |
733 | ||
734 | g: -15, | |
735 | h: -16, | |
736 | i: -17, | |
737 | j: -18, | |
738 | k: -19, | |
739 | ||
740 | l: 'y', | |
741 | m: "def".to_string(), | |
742 | n: -20.5, | |
743 | o: -21.5, | |
744 | p: true, | |
745 | q: Some(1234567), | |
746 | }]); | |
747 | } | |
748 | ||
749 | #[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)] | |
750 | enum Enum { | |
751 | Variant1, | |
752 | Variant2(usize, f32), | |
753 | Variant3 { | |
754 | a: i32, | |
755 | b: char, | |
756 | c: bool, | |
757 | }, | |
758 | } | |
759 | ||
760 | #[test] | |
761 | fn test_enum() { | |
762 | check_round_trip(vec![Enum::Variant1, | |
763 | Enum::Variant2(1, 2.5), | |
764 | Enum::Variant3 { | |
765 | a: 3, | |
766 | b: 'b', | |
767 | c: false, | |
768 | }, | |
769 | Enum::Variant3 { | |
770 | a: -4, | |
771 | b: 'f', | |
772 | c: true, | |
773 | }]); | |
774 | } | |
775 | ||
776 | #[test] | |
777 | fn test_sequence() { | |
778 | let mut vec = vec![]; | |
779 | for i in -100i64..100i64 { | |
780 | vec.push(i * 100000); | |
781 | } | |
782 | ||
783 | check_round_trip(vec![vec]); | |
784 | } | |
785 | ||
786 | #[test] | |
787 | fn test_hash_map() { | |
788 | use std::collections::HashMap; | |
789 | let mut map = HashMap::new(); | |
790 | for i in -100i64..100i64 { | |
791 | map.insert(i * 100000, i * 10000); | |
792 | } | |
793 | ||
794 | check_round_trip(vec![map]); | |
795 | } | |
796 | ||
797 | #[test] | |
798 | fn test_tuples() { | |
799 | check_round_trip(vec![('x', (), false, 0.5f32)]); | |
800 | check_round_trip(vec![(9i8, 10u16, 1.5f64)]); | |
801 | check_round_trip(vec![(-12i16, 11u8, 12usize)]); | |
802 | check_round_trip(vec![(1234567isize, 100000000000000u64, 99999999999999i64)]); | |
803 | check_round_trip(vec![(String::new(), "some string".to_string())]); | |
804 | } | |
805 | } |