(index<- )        ./libserialize/serialize.rs

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


libserialize/serialize.rs:93:1-93:1 -trait- definition:
pub trait Decoder<E> {
    // Primitive types:
    fn read_nil(&mut self) -> Result<(), E>;
references:- 67
libserialize/collection_impls.rs:
libserialize/ebml.rs:
libserialize/json.rs:
libserialize/serialize.rs:


libserialize/serialize.rs:165:1-165:1 -trait- definition:
pub trait Encodable<S:Encoder<E>, E> {
    fn encode(&self, s: &mut S) -> Result<(), E>;
}
references:- 148
libserialize/collection_impls.rs:
libserialize/json.rs:
libserialize/serialize.rs:


libserialize/serialize.rs:584:1-584:1 -trait- definition:
pub trait EncoderHelpers<E> {
    fn emit_from_vec<T>(&mut self,
                        v: &[T],
references:- 2
591: impl<E, S:Encoder<E>> EncoderHelpers<E> for S {
592:     fn emit_from_vec<T>(&mut self, v: &[T], f: |&mut S, &T| -> Result<(), E>) -> Result<(), E> {


libserialize/serialize.rs:169:1-169:1 -trait- definition:
pub trait Decodable<D:Decoder<E>, E> {
    fn decode(d: &mut D) -> Result<Self, E>;
}
references:- 141
libserialize/collection_impls.rs:
libserialize/serialize.rs:


libserialize/serialize.rs:19:1-19:1 -trait- definition:
pub trait Encoder<E> {
    // Primitive types:
    fn emit_nil(&mut self) -> Result<(), E>;
references:- 73
libserialize/collection_impls.rs:
libserialize/ebml.rs:
libserialize/json.rs:
libserialize/serialize.rs:


libserialize/serialize.rs:603:1-603:1 -trait- definition:
pub trait DecoderHelpers<E> {
    fn read_to_vec<T>(&mut self, f: |&mut Self| -> Result<T, E>) -> Result<Vec<T>, E>;
}
references:- 2
604: pub trait DecoderHelpers<E> {
605:     fn read_to_vec<T>(&mut self, f: |&mut Self| -> Result<T, E>) -> Result<Vec<T>, E>;
606: }
608: impl<E, D:Decoder<E>> DecoderHelpers<E> for D {
609:     fn read_to_vec<T>(&mut self, f: |&mut D| -> Result<T, E>) -> Result<Vec<T>, E> {