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