]> git.lizzy.rs Git - rust.git/blob - src/libserialize/serialize.rs
Rollup merge of #31031 - brson:issue-30123, r=nikomatsakis
[rust.git] / src / libserialize / serialize.rs
1 // Copyright 2012-2014 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 //! Support code for encoding and decoding types.
12
13 /*
14 Core encoding and decoding interfaces.
15 */
16
17 use std::path;
18 use std::rc::Rc;
19 use std::cell::{Cell, RefCell};
20 use std::sync::Arc;
21
22 pub trait Encoder {
23     type Error;
24
25     // Primitive types:
26     fn emit_nil(&mut self) -> Result<(), Self::Error>;
27     fn emit_uint(&mut self, v: usize) -> Result<(), Self::Error>;
28     fn emit_u64(&mut self, v: u64) -> Result<(), Self::Error>;
29     fn emit_u32(&mut self, v: u32) -> Result<(), Self::Error>;
30     fn emit_u16(&mut self, v: u16) -> Result<(), Self::Error>;
31     fn emit_u8(&mut self, v: u8) -> Result<(), Self::Error>;
32     fn emit_int(&mut self, v: isize) -> Result<(), Self::Error>;
33     fn emit_i64(&mut self, v: i64) -> Result<(), Self::Error>;
34     fn emit_i32(&mut self, v: i32) -> Result<(), Self::Error>;
35     fn emit_i16(&mut self, v: i16) -> Result<(), Self::Error>;
36     fn emit_i8(&mut self, v: i8) -> Result<(), Self::Error>;
37     fn emit_bool(&mut self, v: bool) -> Result<(), Self::Error>;
38     fn emit_f64(&mut self, v: f64) -> Result<(), Self::Error>;
39     fn emit_f32(&mut self, v: f32) -> Result<(), Self::Error>;
40     fn emit_char(&mut self, v: char) -> Result<(), Self::Error>;
41     fn emit_str(&mut self, v: &str) -> Result<(), Self::Error>;
42
43     // Compound types:
44     fn emit_enum<F>(&mut self, name: &str, f: F) -> Result<(), Self::Error>
45         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
46
47     fn emit_enum_variant<F>(&mut self, v_name: &str,
48                             v_id: usize,
49                             len: usize,
50                             f: F) -> Result<(), Self::Error>
51         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
52     fn emit_enum_variant_arg<F>(&mut self, a_idx: usize, f: F)
53                                 -> Result<(), Self::Error>
54         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
55
56     fn emit_enum_struct_variant<F>(&mut self, v_name: &str,
57                                    v_id: usize,
58                                    len: usize,
59                                    f: F) -> Result<(), Self::Error>
60         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
61     fn emit_enum_struct_variant_field<F>(&mut self,
62                                          f_name: &str,
63                                          f_idx: usize,
64                                          f: F) -> Result<(), Self::Error>
65         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
66
67     fn emit_struct<F>(&mut self, name: &str, len: usize, f: F)
68                       -> Result<(), Self::Error>
69         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
70     fn emit_struct_field<F>(&mut self, f_name: &str, f_idx: usize, f: F)
71                             -> Result<(), Self::Error>
72         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
73
74     fn emit_tuple<F>(&mut self, len: usize, f: F) -> Result<(), Self::Error>
75         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
76     fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> Result<(), Self::Error>
77         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
78
79     fn emit_tuple_struct<F>(&mut self, name: &str, len: usize, f: F)
80                             -> Result<(), Self::Error>
81         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
82     fn emit_tuple_struct_arg<F>(&mut self, f_idx: usize, f: F)
83                                 -> Result<(), Self::Error>
84         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
85
86     // Specialized types:
87     fn emit_option<F>(&mut self, f: F) -> Result<(), Self::Error>
88         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
89     fn emit_option_none(&mut self) -> Result<(), Self::Error>;
90     fn emit_option_some<F>(&mut self, f: F) -> Result<(), Self::Error>
91         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
92
93     fn emit_seq<F>(&mut self, len: usize, f: F) -> Result<(), Self::Error>
94         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
95     fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> Result<(), Self::Error>
96         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
97
98     fn emit_map<F>(&mut self, len: usize, f: F) -> Result<(), Self::Error>
99         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
100     fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> Result<(), Self::Error>
101         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
102     fn emit_map_elt_val<F>(&mut self, idx: usize, f: F) -> Result<(), Self::Error>
103         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
104 }
105
106 pub trait Decoder {
107     type Error;
108
109     // Primitive types:
110     fn read_nil(&mut self) -> Result<(), Self::Error>;
111     fn read_uint(&mut self) -> Result<usize, Self::Error>;
112     fn read_u64(&mut self) -> Result<u64, Self::Error>;
113     fn read_u32(&mut self) -> Result<u32, Self::Error>;
114     fn read_u16(&mut self) -> Result<u16, Self::Error>;
115     fn read_u8(&mut self) -> Result<u8, Self::Error>;
116     fn read_int(&mut self) -> Result<isize, Self::Error>;
117     fn read_i64(&mut self) -> Result<i64, Self::Error>;
118     fn read_i32(&mut self) -> Result<i32, Self::Error>;
119     fn read_i16(&mut self) -> Result<i16, Self::Error>;
120     fn read_i8(&mut self) -> Result<i8, Self::Error>;
121     fn read_bool(&mut self) -> Result<bool, Self::Error>;
122     fn read_f64(&mut self) -> Result<f64, Self::Error>;
123     fn read_f32(&mut self) -> Result<f32, Self::Error>;
124     fn read_char(&mut self) -> Result<char, Self::Error>;
125     fn read_str(&mut self) -> Result<String, Self::Error>;
126
127     // Compound types:
128     fn read_enum<T, F>(&mut self, name: &str, f: F) -> Result<T, Self::Error>
129         where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
130
131     fn read_enum_variant<T, F>(&mut self, names: &[&str], f: F)
132                                -> Result<T, Self::Error>
133         where F: FnMut(&mut Self, usize) -> Result<T, Self::Error>;
134     fn read_enum_variant_arg<T, F>(&mut self, a_idx: usize, f: F)
135                                    -> Result<T, Self::Error>
136         where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
137
138     fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F)
139                                       -> Result<T, Self::Error>
140         where F: FnMut(&mut Self, usize) -> Result<T, Self::Error>;
141     fn read_enum_struct_variant_field<T, F>(&mut self,
142                                             &f_name: &str,
143                                             f_idx: usize,
144                                             f: F)
145                                             -> Result<T, Self::Error>
146         where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
147
148     fn read_struct<T, F>(&mut self, s_name: &str, len: usize, f: F)
149                          -> Result<T, Self::Error>
150         where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
151     fn read_struct_field<T, F>(&mut self,
152                                f_name: &str,
153                                f_idx: usize,
154                                f: F)
155                                -> Result<T, Self::Error>
156         where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
157
158     fn read_tuple<T, F>(&mut self, len: usize, f: F) -> Result<T, Self::Error>
159         where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
160     fn read_tuple_arg<T, F>(&mut self, a_idx: usize, f: F)
161                             -> Result<T, Self::Error>
162         where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
163
164     fn read_tuple_struct<T, F>(&mut self, s_name: &str, len: usize, f: F)
165                                -> Result<T, Self::Error>
166         where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
167     fn read_tuple_struct_arg<T, F>(&mut self, a_idx: usize, f: F)
168                                    -> Result<T, Self::Error>
169         where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
170
171     // Specialized types:
172     fn read_option<T, F>(&mut self, f: F) -> Result<T, Self::Error>
173         where F: FnMut(&mut Self, bool) -> Result<T, Self::Error>;
174
175     fn read_seq<T, F>(&mut self, f: F) -> Result<T, Self::Error>
176         where F: FnOnce(&mut Self, usize) -> Result<T, Self::Error>;
177     fn read_seq_elt<T, F>(&mut self, idx: usize, f: F) -> Result<T, Self::Error>
178         where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
179
180     fn read_map<T, F>(&mut self, f: F) -> Result<T, Self::Error>
181         where F: FnOnce(&mut Self, usize) -> Result<T, Self::Error>;
182     fn read_map_elt_key<T, F>(&mut self, idx: usize, f: F)
183                               -> Result<T, Self::Error>
184         where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
185     fn read_map_elt_val<T, F>(&mut self, idx: usize, f: F)
186                               -> Result<T, Self::Error>
187         where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
188
189     // Failure
190     fn error(&mut self, err: &str) -> Self::Error;
191 }
192
193 pub trait Encodable {
194     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>;
195 }
196
197 pub trait Decodable: Sized {
198     fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error>;
199 }
200
201 impl Encodable for usize {
202     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
203         s.emit_uint(*self)
204     }
205 }
206
207 impl Decodable for usize {
208     fn decode<D: Decoder>(d: &mut D) -> Result<usize, D::Error> {
209         d.read_uint()
210     }
211 }
212
213 impl Encodable for u8 {
214     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
215         s.emit_u8(*self)
216     }
217 }
218
219 impl Decodable for u8 {
220     fn decode<D: Decoder>(d: &mut D) -> Result<u8, D::Error> {
221         d.read_u8()
222     }
223 }
224
225 impl Encodable for u16 {
226     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
227         s.emit_u16(*self)
228     }
229 }
230
231 impl Decodable for u16 {
232     fn decode<D: Decoder>(d: &mut D) -> Result<u16, D::Error> {
233         d.read_u16()
234     }
235 }
236
237 impl Encodable for u32 {
238     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
239         s.emit_u32(*self)
240     }
241 }
242
243 impl Decodable for u32 {
244     fn decode<D: Decoder>(d: &mut D) -> Result<u32, D::Error> {
245         d.read_u32()
246     }
247 }
248
249 impl Encodable for u64 {
250     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
251         s.emit_u64(*self)
252     }
253 }
254
255 impl Decodable for u64 {
256     fn decode<D: Decoder>(d: &mut D) -> Result<u64, D::Error> {
257         d.read_u64()
258     }
259 }
260
261 impl Encodable for isize {
262     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
263         s.emit_int(*self)
264     }
265 }
266
267 impl Decodable for isize {
268     fn decode<D: Decoder>(d: &mut D) -> Result<isize, D::Error> {
269         d.read_int()
270     }
271 }
272
273 impl Encodable for i8 {
274     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
275         s.emit_i8(*self)
276     }
277 }
278
279 impl Decodable for i8 {
280     fn decode<D: Decoder>(d: &mut D) -> Result<i8, D::Error> {
281         d.read_i8()
282     }
283 }
284
285 impl Encodable for i16 {
286     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
287         s.emit_i16(*self)
288     }
289 }
290
291 impl Decodable for i16 {
292     fn decode<D: Decoder>(d: &mut D) -> Result<i16, D::Error> {
293         d.read_i16()
294     }
295 }
296
297 impl Encodable for i32 {
298     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
299         s.emit_i32(*self)
300     }
301 }
302
303 impl Decodable for i32 {
304     fn decode<D: Decoder>(d: &mut D) -> Result<i32, D::Error> {
305         d.read_i32()
306     }
307 }
308
309 impl Encodable for i64 {
310     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
311         s.emit_i64(*self)
312     }
313 }
314
315 impl Decodable for i64 {
316     fn decode<D: Decoder>(d: &mut D) -> Result<i64, D::Error> {
317         d.read_i64()
318     }
319 }
320
321 impl Encodable for str {
322     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
323         s.emit_str(self)
324     }
325 }
326
327 impl Encodable for String {
328     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
329         s.emit_str(&self[..])
330     }
331 }
332
333 impl Decodable for String {
334     fn decode<D: Decoder>(d: &mut D) -> Result<String, D::Error> {
335         d.read_str()
336     }
337 }
338
339 impl Encodable for f32 {
340     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
341         s.emit_f32(*self)
342     }
343 }
344
345 impl Decodable for f32 {
346     fn decode<D: Decoder>(d: &mut D) -> Result<f32, D::Error> {
347         d.read_f32()
348     }
349 }
350
351 impl Encodable for f64 {
352     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
353         s.emit_f64(*self)
354     }
355 }
356
357 impl Decodable for f64 {
358     fn decode<D: Decoder>(d: &mut D) -> Result<f64, D::Error> {
359         d.read_f64()
360     }
361 }
362
363 impl Encodable for bool {
364     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
365         s.emit_bool(*self)
366     }
367 }
368
369 impl Decodable for bool {
370     fn decode<D: Decoder>(d: &mut D) -> Result<bool, D::Error> {
371         d.read_bool()
372     }
373 }
374
375 impl Encodable for char {
376     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
377         s.emit_char(*self)
378     }
379 }
380
381 impl Decodable for char {
382     fn decode<D: Decoder>(d: &mut D) -> Result<char, D::Error> {
383         d.read_char()
384     }
385 }
386
387 impl Encodable for () {
388     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
389         s.emit_nil()
390     }
391 }
392
393 impl Decodable for () {
394     fn decode<D: Decoder>(d: &mut D) -> Result<(), D::Error> {
395         d.read_nil()
396     }
397 }
398
399 impl<'a, T: ?Sized + Encodable> Encodable for &'a T {
400     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
401         (**self).encode(s)
402     }
403 }
404
405 impl<T: ?Sized + Encodable> Encodable for Box<T> {
406     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
407         (**self).encode(s)
408     }
409 }
410
411 impl< T: Decodable> Decodable for Box<T> {
412     fn decode<D: Decoder>(d: &mut D) -> Result<Box<T>, D::Error> {
413         Ok(box try!(Decodable::decode(d)))
414     }
415 }
416
417 impl< T: Decodable> Decodable for Box<[T]> {
418     fn decode<D: Decoder>(d: &mut D) -> Result<Box<[T]>, D::Error> {
419         let v: Vec<T> = try!(Decodable::decode(d));
420         Ok(v.into_boxed_slice())
421     }
422 }
423
424 impl<T:Encodable> Encodable for Rc<T> {
425     #[inline]
426     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
427         (**self).encode(s)
428     }
429 }
430
431 impl<T:Decodable> Decodable for Rc<T> {
432     #[inline]
433     fn decode<D: Decoder>(d: &mut D) -> Result<Rc<T>, D::Error> {
434         Ok(Rc::new(try!(Decodable::decode(d))))
435     }
436 }
437
438 impl<T:Encodable> Encodable for [T] {
439     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
440         s.emit_seq(self.len(), |s| {
441             for (i, e) in self.iter().enumerate() {
442                 try!(s.emit_seq_elt(i, |s| e.encode(s)))
443             }
444             Ok(())
445         })
446     }
447 }
448
449 impl<T:Encodable> Encodable for Vec<T> {
450     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
451         s.emit_seq(self.len(), |s| {
452             for (i, e) in self.iter().enumerate() {
453                 try!(s.emit_seq_elt(i, |s| e.encode(s)))
454             }
455             Ok(())
456         })
457     }
458 }
459
460 impl<T:Decodable> Decodable for Vec<T> {
461     fn decode<D: Decoder>(d: &mut D) -> Result<Vec<T>, D::Error> {
462         d.read_seq(|d, len| {
463             let mut v = Vec::with_capacity(len);
464             for i in 0..len {
465                 v.push(try!(d.read_seq_elt(i, |d| Decodable::decode(d))));
466             }
467             Ok(v)
468         })
469     }
470 }
471
472 impl<T:Encodable> Encodable for Option<T> {
473     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
474         s.emit_option(|s| {
475             match *self {
476                 None => s.emit_option_none(),
477                 Some(ref v) => s.emit_option_some(|s| v.encode(s)),
478             }
479         })
480     }
481 }
482
483 impl<T:Decodable> Decodable for Option<T> {
484     fn decode<D: Decoder>(d: &mut D) -> Result<Option<T>, D::Error> {
485         d.read_option(|d, b| {
486             if b {
487                 Ok(Some(try!(Decodable::decode(d))))
488             } else {
489                 Ok(None)
490             }
491         })
492     }
493 }
494
495 macro_rules! peel {
496     ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
497 }
498
499 /// Evaluates to the number of identifiers passed to it, for example: `count_idents!(a, b, c) == 3
500 macro_rules! count_idents {
501     () => { 0 };
502     ($_i:ident, $($rest:ident,)*) => { 1 + count_idents!($($rest,)*) }
503 }
504
505 macro_rules! tuple {
506     () => ();
507     ( $($name:ident,)+ ) => (
508         impl<$($name:Decodable),*> Decodable for ($($name,)*) {
509             #[allow(non_snake_case)]
510             fn decode<D: Decoder>(d: &mut D) -> Result<($($name,)*), D::Error> {
511                 let len: usize = count_idents!($($name,)*);
512                 d.read_tuple(len, |d| {
513                     let mut i = 0;
514                     let ret = ($(try!(d.read_tuple_arg({ i+=1; i-1 },
515                                                        |d| -> Result<$name,D::Error> {
516                         Decodable::decode(d)
517                     })),)*);
518                     Ok(ret)
519                 })
520             }
521         }
522         impl<$($name:Encodable),*> Encodable for ($($name,)*) {
523             #[allow(non_snake_case)]
524             fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
525                 let ($(ref $name,)*) = *self;
526                 let mut n = 0;
527                 $(let $name = $name; n += 1;)*
528                 s.emit_tuple(n, |s| {
529                     let mut i = 0;
530                     $(try!(s.emit_tuple_arg({ i+=1; i-1 }, |s| $name.encode(s)));)*
531                     Ok(())
532                 })
533             }
534         }
535         peel! { $($name,)* }
536     )
537 }
538
539 tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
540
541 impl Encodable for path::PathBuf {
542     fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
543         self.to_str().unwrap().encode(e)
544     }
545 }
546
547 impl Decodable for path::PathBuf {
548     fn decode<D: Decoder>(d: &mut D) -> Result<path::PathBuf, D::Error> {
549         let bytes: String = try!(Decodable::decode(d));
550         Ok(path::PathBuf::from(bytes))
551     }
552 }
553
554 impl<T: Encodable + Copy> Encodable for Cell<T> {
555     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
556         self.get().encode(s)
557     }
558 }
559
560 impl<T: Decodable + Copy> Decodable for Cell<T> {
561     fn decode<D: Decoder>(d: &mut D) -> Result<Cell<T>, D::Error> {
562         Ok(Cell::new(try!(Decodable::decode(d))))
563     }
564 }
565
566 // FIXME: #15036
567 // Should use `try_borrow`, returning a
568 // `encoder.error("attempting to Encode borrowed RefCell")`
569 // from `encode` when `try_borrow` returns `None`.
570
571 impl<T: Encodable> Encodable for RefCell<T> {
572     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
573         self.borrow().encode(s)
574     }
575 }
576
577 impl<T: Decodable> Decodable for RefCell<T> {
578     fn decode<D: Decoder>(d: &mut D) -> Result<RefCell<T>, D::Error> {
579         Ok(RefCell::new(try!(Decodable::decode(d))))
580     }
581 }
582
583 impl<T:Encodable> Encodable for Arc<T> {
584     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
585         (**self).encode(s)
586     }
587 }
588
589 impl<T:Decodable+Send+Sync> Decodable for Arc<T> {
590     fn decode<D: Decoder>(d: &mut D) -> Result<Arc<T>, D::Error> {
591         Ok(Arc::new(try!(Decodable::decode(d))))
592     }
593 }
594
595 // ___________________________________________________________________________
596 // Helper routines
597
598 pub trait EncoderHelpers: Encoder {
599     fn emit_from_vec<T, F>(&mut self, v: &[T], f: F)
600                            -> Result<(), Self::Error>
601         where F: FnMut(&mut Self, &T) -> Result<(), Self::Error>;
602 }
603
604 impl<S:Encoder> EncoderHelpers for S {
605     fn emit_from_vec<T, F>(&mut self, v: &[T], mut f: F) -> Result<(), S::Error> where
606         F: FnMut(&mut S, &T) -> Result<(), S::Error>,
607     {
608         self.emit_seq(v.len(), |this| {
609             for (i, e) in v.iter().enumerate() {
610                 try!(this.emit_seq_elt(i, |this| {
611                     f(this, e)
612                 }));
613             }
614             Ok(())
615         })
616     }
617 }
618
619 pub trait DecoderHelpers: Decoder {
620     fn read_to_vec<T, F>(&mut self, f: F)
621                          -> Result<Vec<T>, Self::Error> where
622         F: FnMut(&mut Self) -> Result<T, Self::Error>;
623 }
624
625 impl<D: Decoder> DecoderHelpers for D {
626     fn read_to_vec<T, F>(&mut self, mut f: F) -> Result<Vec<T>, D::Error> where F:
627         FnMut(&mut D) -> Result<T, D::Error>,
628     {
629         self.read_seq(|this, len| {
630             let mut v = Vec::with_capacity(len);
631             for i in 0..len {
632                 v.push(try!(this.read_seq_elt(i, |this| f(this))));
633             }
634             Ok(v)
635         })
636     }
637 }