]> git.lizzy.rs Git - rust.git/blob - src/libserialize/serialize.rs
Fix misspelled comments.
[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: uint) -> 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: int) -> 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: uint,
49                             len: uint,
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: uint, 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: uint,
58                                    len: uint,
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: uint,
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: uint, 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: uint, f: F)
71                             -> Result<(), Self::Error>
72         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
73
74     fn emit_tuple<F>(&mut self, len: uint, f: F) -> Result<(), Self::Error>
75         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
76     fn emit_tuple_arg<F>(&mut self, idx: uint, 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: uint, 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: uint, 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: uint, f: F) -> Result<(), Self::Error>
94         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
95     fn emit_seq_elt<F>(&mut self, idx: uint, f: F) -> Result<(), Self::Error>
96         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
97
98     fn emit_map<F>(&mut self, len: uint, f: F) -> Result<(), Self::Error>
99         where F: FnOnce(&mut Self) -> Result<(), Self::Error>;
100     fn emit_map_elt_key<F>(&mut self, idx: uint, f: F) -> Result<(), Self::Error>
101         where F: FnMut(&mut Self) -> Result<(), Self::Error>;
102     fn emit_map_elt_val<F>(&mut self, idx: uint, 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<uint, 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<int, 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, uint) -> Result<T, Self::Error>;
134     fn read_enum_variant_arg<T, F>(&mut self, a_idx: uint, 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, uint) -> Result<T, Self::Error>;
141     fn read_enum_struct_variant_field<T, F>(&mut self,
142                                             &f_name: &str,
143                                             f_idx: uint,
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: uint, 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: uint,
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: uint, 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: uint, 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: uint, 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: uint, 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, uint) -> Result<T, Self::Error>;
177     fn read_seq_elt<T, F>(&mut self, idx: uint, 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, uint) -> Result<T, Self::Error>;
182     fn read_map_elt_key<T, F>(&mut self, idx: uint, 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: uint, 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 {
198     fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error>;
199 }
200
201 impl Encodable for uint {
202     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
203         s.emit_uint(*self)
204     }
205 }
206
207 impl Decodable for uint {
208     fn decode<D: Decoder>(d: &mut D) -> Result<uint, 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 int {
262     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
263         s.emit_int(*self)
264     }
265 }
266
267 impl Decodable for int {
268     fn decode<D: Decoder>(d: &mut D) -> Result<int, 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 range(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     () => { 0u };
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: uint = 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                     return 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::posix::Path {
542     fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
543         self.as_vec().encode(e)
544     }
545 }
546
547 impl Decodable for path::posix::Path {
548     fn decode<D: Decoder>(d: &mut D) -> Result<path::posix::Path, D::Error> {
549         let bytes: Vec<u8> = try!(Decodable::decode(d));
550         Ok(path::posix::Path::new(bytes))
551     }
552 }
553
554 impl Encodable for path::windows::Path {
555     fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
556         self.as_vec().encode(e)
557     }
558 }
559
560 impl Decodable for path::windows::Path {
561     fn decode<D: Decoder>(d: &mut D) -> Result<path::windows::Path, D::Error> {
562         let bytes: Vec<u8> = try!(Decodable::decode(d));
563         Ok(path::windows::Path::new(bytes))
564     }
565 }
566
567 impl<T: Encodable + Copy> Encodable for Cell<T> {
568     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
569         self.get().encode(s)
570     }
571 }
572
573 impl<T: Decodable + Copy> Decodable for Cell<T> {
574     fn decode<D: Decoder>(d: &mut D) -> Result<Cell<T>, D::Error> {
575         Ok(Cell::new(try!(Decodable::decode(d))))
576     }
577 }
578
579 // FIXME: #15036
580 // Should use `try_borrow`, returning a
581 // `encoder.error("attempting to Encode borrowed RefCell")`
582 // from `encode` when `try_borrow` returns `None`.
583
584 impl<T: Encodable> Encodable for RefCell<T> {
585     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
586         self.borrow().encode(s)
587     }
588 }
589
590 impl<T: Decodable> Decodable for RefCell<T> {
591     fn decode<D: Decoder>(d: &mut D) -> Result<RefCell<T>, D::Error> {
592         Ok(RefCell::new(try!(Decodable::decode(d))))
593     }
594 }
595
596 impl<T:Encodable> Encodable for Arc<T> {
597     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
598         (**self).encode(s)
599     }
600 }
601
602 impl<T:Decodable+Send+Sync> Decodable for Arc<T> {
603     fn decode<D: Decoder>(d: &mut D) -> Result<Arc<T>, D::Error> {
604         Ok(Arc::new(try!(Decodable::decode(d))))
605     }
606 }
607
608 // ___________________________________________________________________________
609 // Helper routines
610
611 pub trait EncoderHelpers: Encoder {
612     fn emit_from_vec<T, F>(&mut self, v: &[T], f: F)
613                            -> Result<(), <Self as Encoder>::Error>
614         where F: FnMut(&mut Self, &T) -> Result<(), <Self as Encoder>::Error>;
615 }
616
617 impl<S:Encoder> EncoderHelpers for S {
618     fn emit_from_vec<T, F>(&mut self, v: &[T], mut f: F) -> Result<(), S::Error> where
619         F: FnMut(&mut S, &T) -> Result<(), S::Error>,
620     {
621         self.emit_seq(v.len(), |this| {
622             for (i, e) in v.iter().enumerate() {
623                 try!(this.emit_seq_elt(i, |this| {
624                     f(this, e)
625                 }));
626             }
627             Ok(())
628         })
629     }
630 }
631
632 pub trait DecoderHelpers: Decoder {
633     fn read_to_vec<T, F>(&mut self, f: F)
634                          -> Result<Vec<T>, <Self as Decoder>::Error> where
635         F: FnMut(&mut Self) -> Result<T, <Self as Decoder>::Error>;
636 }
637
638 impl<D: Decoder> DecoderHelpers for D {
639     fn read_to_vec<T, F>(&mut self, mut f: F) -> Result<Vec<T>, D::Error> where F:
640         FnMut(&mut D) -> Result<T, D::Error>,
641     {
642         self.read_seq(|this, len| {
643             let mut v = Vec::with_capacity(len);
644             for i in range(0, len) {
645                 v.push(try!(this.read_seq_elt(i, |this| f(this))));
646             }
647             Ok(v)
648         })
649     }
650 }