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.
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.
11 //! Support code for encoding and decoding types.
14 Core encoding and decoding interfaces.
20 pub trait Encoder<E> {
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>;
40 fn emit_enum(&mut self, name: &str, f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
42 fn emit_enum_variant(&mut self,
46 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
47 fn emit_enum_variant_arg(&mut self,
49 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
51 fn emit_enum_struct_variant(&mut self,
55 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
56 fn emit_enum_struct_variant_field(&mut self,
59 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
61 fn emit_struct(&mut self,
64 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
65 fn emit_struct_field(&mut self,
68 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
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>;
73 fn emit_tuple_struct(&mut self,
76 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
77 fn emit_tuple_struct_arg(&mut self,
79 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
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>;
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>;
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>;
94 pub trait Decoder<E> {
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<StrBuf, E>;
114 fn read_enum<T>(&mut self, name: &str, f: |&mut Self| -> Result<T, E>) -> Result<T, E>;
116 fn read_enum_variant<T>(&mut self,
118 f: |&mut Self, uint| -> Result<T, E>)
120 fn read_enum_variant_arg<T>(&mut self,
122 f: |&mut Self| -> Result<T, E>)
125 fn read_enum_struct_variant<T>(&mut self,
127 f: |&mut Self, uint| -> Result<T, E>)
129 fn read_enum_struct_variant_field<T>(&mut self,
132 f: |&mut Self| -> Result<T, E>)
135 fn read_struct<T>(&mut self, s_name: &str, len: uint, f: |&mut Self| -> Result<T, E>)
137 fn read_struct_field<T>(&mut self,
140 f: |&mut Self| -> Result<T, E>)
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>;
146 fn read_tuple_struct<T>(&mut self,
148 f: |&mut Self, uint| -> Result<T, E>)
150 fn read_tuple_struct_arg<T>(&mut self,
152 f: |&mut Self| -> Result<T, E>)
155 // Specialized types:
156 fn read_option<T>(&mut self, f: |&mut Self, bool| -> Result<T, E>) -> Result<T, E>;
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>;
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>;
166 pub trait Encodable<S:Encoder<E>, E> {
167 fn encode(&self, s: &mut S) -> Result<(), E>;
170 pub trait Decodable<D:Decoder<E>, E> {
171 fn decode(d: &mut D) -> Result<Self, E>;
174 impl<E, S:Encoder<E>> Encodable<S, E> for uint {
175 fn encode(&self, s: &mut S) -> Result<(), E> {
180 impl<E, D:Decoder<E>> Decodable<D, E> for uint {
181 fn decode(d: &mut D) -> Result<uint, E> {
186 impl<E, S:Encoder<E>> Encodable<S, E> for u8 {
187 fn encode(&self, s: &mut S) -> Result<(), E> {
192 impl<E, D:Decoder<E>> Decodable<D, E> for u8 {
193 fn decode(d: &mut D) -> Result<u8, E> {
198 impl<E, S:Encoder<E>> Encodable<S, E> for u16 {
199 fn encode(&self, s: &mut S) -> Result<(), E> {
204 impl<E, D:Decoder<E>> Decodable<D, E> for u16 {
205 fn decode(d: &mut D) -> Result<u16, E> {
210 impl<E, S:Encoder<E>> Encodable<S, E> for u32 {
211 fn encode(&self, s: &mut S) -> Result<(), E> {
216 impl<E, D:Decoder<E>> Decodable<D, E> for u32 {
217 fn decode(d: &mut D) -> Result<u32, E> {
222 impl<E, S:Encoder<E>> Encodable<S, E> for u64 {
223 fn encode(&self, s: &mut S) -> Result<(), E> {
228 impl<E, D:Decoder<E>> Decodable<D, E> for u64 {
229 fn decode(d: &mut D) -> Result<u64, E> {
234 impl<E, S:Encoder<E>> Encodable<S, E> for int {
235 fn encode(&self, s: &mut S) -> Result<(), E> {
240 impl<E, D:Decoder<E>> Decodable<D, E> for int {
241 fn decode(d: &mut D) -> Result<int, E> {
246 impl<E, S:Encoder<E>> Encodable<S, E> for i8 {
247 fn encode(&self, s: &mut S) -> Result<(), E> {
252 impl<E, D:Decoder<E>> Decodable<D, E> for i8 {
253 fn decode(d: &mut D) -> Result<i8, E> {
258 impl<E, S:Encoder<E>> Encodable<S, E> for i16 {
259 fn encode(&self, s: &mut S) -> Result<(), E> {
264 impl<E, D:Decoder<E>> Decodable<D, E> for i16 {
265 fn decode(d: &mut D) -> Result<i16, E> {
270 impl<E, S:Encoder<E>> Encodable<S, E> for i32 {
271 fn encode(&self, s: &mut S) -> Result<(), E> {
276 impl<E, D:Decoder<E>> Decodable<D, E> for i32 {
277 fn decode(d: &mut D) -> Result<i32, E> {
282 impl<E, S:Encoder<E>> Encodable<S, E> for i64 {
283 fn encode(&self, s: &mut S) -> Result<(), E> {
288 impl<E, D:Decoder<E>> Decodable<D, E> for i64 {
289 fn decode(d: &mut D) -> Result<i64, E> {
294 impl<'a, E, S:Encoder<E>> Encodable<S, E> for &'a str {
295 fn encode(&self, s: &mut S) -> Result<(), E> {
300 impl<E, S:Encoder<E>> Encodable<S, E> for StrBuf {
301 fn encode(&self, s: &mut S) -> Result<(), E> {
302 s.emit_str(self.as_slice())
306 impl<E, D:Decoder<E>> Decodable<D, E> for StrBuf {
307 fn decode(d: &mut D) -> Result<StrBuf, E> {
308 Ok(StrBuf::from_str(try!(d.read_str()).as_slice()))
312 impl<E, S:Encoder<E>> Encodable<S, E> for f32 {
313 fn encode(&self, s: &mut S) -> Result<(), E> {
318 impl<E, D:Decoder<E>> Decodable<D, E> for f32 {
319 fn decode(d: &mut D) -> Result<f32, E> {
324 impl<E, S:Encoder<E>> Encodable<S, E> for f64 {
325 fn encode(&self, s: &mut S) -> Result<(), E> {
330 impl<E, D:Decoder<E>> Decodable<D, E> for f64 {
331 fn decode(d: &mut D) -> Result<f64, E> {
336 impl<E, S:Encoder<E>> Encodable<S, E> for bool {
337 fn encode(&self, s: &mut S) -> Result<(), E> {
342 impl<E, D:Decoder<E>> Decodable<D, E> for bool {
343 fn decode(d: &mut D) -> Result<bool, E> {
348 impl<E, S:Encoder<E>> Encodable<S, E> for char {
349 fn encode(&self, s: &mut S) -> Result<(), E> {
354 impl<E, D:Decoder<E>> Decodable<D, E> for char {
355 fn decode(d: &mut D) -> Result<char, E> {
360 impl<E, S:Encoder<E>> Encodable<S, E> for () {
361 fn encode(&self, s: &mut S) -> Result<(), E> {
366 impl<E, D:Decoder<E>> Decodable<D, E> for () {
367 fn decode(d: &mut D) -> Result<(), E> {
372 impl<'a, E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for &'a T {
373 fn encode(&self, s: &mut S) -> Result<(), E> {
378 impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for Box<T> {
379 fn encode(&self, s: &mut S) -> Result<(), E> {
384 impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for Box<T> {
385 fn decode(d: &mut D) -> Result<Box<T>, E> {
386 Ok(box try!(Decodable::decode(d)))
390 impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for @T {
391 fn encode(&self, s: &mut S) -> Result<(), E> {
396 impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for Rc<T> {
398 fn encode(&self, s: &mut S) -> Result<(), E> {
403 impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for Rc<T> {
405 fn decode(d: &mut D) -> Result<Rc<T>, E> {
406 Ok(Rc::new(try!(Decodable::decode(d))))
410 impl<E, D:Decoder<E>,T:Decodable<D, E> + 'static> Decodable<D, E> for @T {
411 fn decode(d: &mut D) -> Result<@T, E> {
412 Ok(@try!(Decodable::decode(d)))
416 impl<'a, E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for &'a [T] {
417 fn encode(&self, s: &mut S) -> Result<(), E> {
418 s.emit_seq(self.len(), |s| {
419 for (i, e) in self.iter().enumerate() {
420 try!(s.emit_seq_elt(i, |s| e.encode(s)))
427 impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for ~[T] {
428 fn encode(&self, s: &mut S) -> Result<(), E> {
429 s.emit_seq(self.len(), |s| {
430 for (i, e) in self.iter().enumerate() {
431 try!(s.emit_seq_elt(i, |s| e.encode(s)))
438 impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for ~[T] {
439 fn decode(d: &mut D) -> Result<~[T], E> {
440 use std::vec::FromVec;
442 d.read_seq(|d, len| {
443 let mut v: Vec<T> = Vec::with_capacity(len);
444 for i in range(0, len) {
445 v.push(try!(d.read_seq_elt(i, |d| Decodable::decode(d))));
447 let k: ~[T] = FromVec::from_vec(v);
453 impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for Vec<T> {
454 fn encode(&self, s: &mut S) -> Result<(), E> {
455 s.emit_seq(self.len(), |s| {
456 for (i, e) in self.iter().enumerate() {
457 try!(s.emit_seq_elt(i, |s| e.encode(s)))
464 impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for Vec<T> {
465 fn decode(d: &mut D) -> Result<Vec<T>, E> {
466 d.read_seq(|d, len| {
467 let mut v = Vec::with_capacity(len);
468 for i in range(0, len) {
469 v.push(try!(d.read_seq_elt(i, |d| Decodable::decode(d))));
476 impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for Option<T> {
477 fn encode(&self, s: &mut S) -> Result<(), E> {
480 None => s.emit_option_none(),
481 Some(ref v) => s.emit_option_some(|s| v.encode(s)),
487 impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for Option<T> {
488 fn decode(d: &mut D) -> Result<Option<T>, E> {
489 d.read_option(|d, b| {
491 Ok(Some(try!(Decodable::decode(d))))
499 macro_rules! peel(($name:ident, $($other:ident,)*) => (tuple!($($other,)*)))
503 ( $($name:ident,)+ ) => (
504 impl<E, D:Decoder<E>,$($name:Decodable<D, E>),*> Decodable<D,E> for ($($name,)*) {
505 #[allow(uppercase_variables)]
506 fn decode(d: &mut D) -> Result<($($name,)*), E> {
507 d.read_tuple(|d, amt| {
509 let ret = ($(try!(d.read_tuple_arg({ i+=1; i-1 }, |d| -> Result<$name,E> {
513 "expected tuple of length `{}`, found tuple \
514 of length `{}`", i, amt);
519 impl<E, S:Encoder<E>,$($name:Encodable<S, E>),*> Encodable<S, E> for ($($name,)*) {
520 #[allow(uppercase_variables)]
521 fn encode(&self, s: &mut S) -> Result<(), E> {
522 let ($(ref $name,)*) = *self;
524 $(let $name = $name; n += 1;)*
525 s.emit_tuple(n, |s| {
527 $(try!(s.emit_seq_elt({ i+=1; i-1 }, |s| $name.encode(s)));)*
536 tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
538 impl<E, S: Encoder<E>> Encodable<S, E> for path::posix::Path {
539 fn encode(&self, e: &mut S) -> Result<(), E> {
540 self.as_vec().encode(e)
544 impl<E, D: Decoder<E>> Decodable<D, E> for path::posix::Path {
545 fn decode(d: &mut D) -> Result<path::posix::Path, E> {
546 let bytes: Vec<u8> = try!(Decodable::decode(d));
547 Ok(path::posix::Path::new(bytes))
551 impl<E, S: Encoder<E>> Encodable<S, E> for path::windows::Path {
552 fn encode(&self, e: &mut S) -> Result<(), E> {
553 self.as_vec().encode(e)
557 impl<E, D: Decoder<E>> Decodable<D, E> for path::windows::Path {
558 fn decode(d: &mut D) -> Result<path::windows::Path, E> {
559 let bytes: Vec<u8> = try!(Decodable::decode(d));
560 Ok(path::windows::Path::new(bytes))
564 // ___________________________________________________________________________
567 // In some cases, these should eventually be coded as traits.
569 pub trait EncoderHelpers<E> {
570 fn emit_from_vec<T>(&mut self,
572 f: |&mut Self, v: &T| -> Result<(), E>) -> Result<(), E>;
575 impl<E, S:Encoder<E>> EncoderHelpers<E> for S {
576 fn emit_from_vec<T>(&mut self, v: &[T], f: |&mut S, &T| -> Result<(), E>) -> Result<(), E> {
577 self.emit_seq(v.len(), |this| {
578 for (i, e) in v.iter().enumerate() {
579 try!(this.emit_seq_elt(i, |this| {
588 pub trait DecoderHelpers<E> {
589 fn read_to_vec<T>(&mut self, f: |&mut Self| -> Result<T, E>) -> Result<Vec<T>, E>;
592 impl<E, D:Decoder<E>> DecoderHelpers<E> for D {
593 fn read_to_vec<T>(&mut self, f: |&mut D| -> Result<T, E>) -> Result<Vec<T>, E> {
594 self.read_seq(|this, len| {
595 let mut v = Vec::with_capacity(len);
596 for i in range(0, len) {
597 v.push(try!(this.read_seq_elt(i, |this| f(this))));