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.
19 use std::cell::{Cell, RefCell};
21 pub trait Encoder<E> {
23 fn emit_nil(&mut self) -> Result<(), E>;
24 fn emit_uint(&mut self, v: uint) -> Result<(), E>;
25 fn emit_u64(&mut self, v: u64) -> Result<(), E>;
26 fn emit_u32(&mut self, v: u32) -> Result<(), E>;
27 fn emit_u16(&mut self, v: u16) -> Result<(), E>;
28 fn emit_u8(&mut self, v: u8) -> Result<(), E>;
29 fn emit_int(&mut self, v: int) -> Result<(), E>;
30 fn emit_i64(&mut self, v: i64) -> Result<(), E>;
31 fn emit_i32(&mut self, v: i32) -> Result<(), E>;
32 fn emit_i16(&mut self, v: i16) -> Result<(), E>;
33 fn emit_i8(&mut self, v: i8) -> Result<(), E>;
34 fn emit_bool(&mut self, v: bool) -> Result<(), E>;
35 fn emit_f64(&mut self, v: f64) -> Result<(), E>;
36 fn emit_f32(&mut self, v: f32) -> Result<(), E>;
37 fn emit_char(&mut self, v: char) -> Result<(), E>;
38 fn emit_str(&mut self, v: &str) -> Result<(), E>;
41 fn emit_enum(&mut self, name: &str, f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
43 fn emit_enum_variant(&mut self,
47 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
48 fn emit_enum_variant_arg(&mut self,
50 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
52 fn emit_enum_struct_variant(&mut self,
56 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
57 fn emit_enum_struct_variant_field(&mut self,
60 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
62 fn emit_struct(&mut self,
65 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
66 fn emit_struct_field(&mut self,
69 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
71 fn emit_tuple(&mut self, len: uint, f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
72 fn emit_tuple_arg(&mut self, idx: uint, f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
74 fn emit_tuple_struct(&mut self,
77 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
78 fn emit_tuple_struct_arg(&mut self,
80 f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
83 fn emit_option(&mut self, f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
84 fn emit_option_none(&mut self) -> Result<(), E>;
85 fn emit_option_some(&mut self, f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
87 fn emit_seq(&mut self, len: uint, f: |this: &mut Self| -> Result<(), E>) -> Result<(), E>;
88 fn emit_seq_elt(&mut self, idx: uint, f: |this: &mut Self| -> Result<(), E>) -> Result<(), E>;
90 fn emit_map(&mut self, len: uint, f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
91 fn emit_map_elt_key(&mut self, idx: uint, f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
92 fn emit_map_elt_val(&mut self, idx: uint, f: |&mut Self| -> Result<(), E>) -> Result<(), E>;
95 pub trait Decoder<E> {
97 fn read_nil(&mut self) -> Result<(), E>;
98 fn read_uint(&mut self) -> Result<uint, E>;
99 fn read_u64(&mut self) -> Result<u64, E>;
100 fn read_u32(&mut self) -> Result<u32, E>;
101 fn read_u16(&mut self) -> Result<u16, E>;
102 fn read_u8(&mut self) -> Result<u8, E>;
103 fn read_int(&mut self) -> Result<int, E>;
104 fn read_i64(&mut self) -> Result<i64, E>;
105 fn read_i32(&mut self) -> Result<i32, E>;
106 fn read_i16(&mut self) -> Result<i16, E>;
107 fn read_i8(&mut self) -> Result<i8, E>;
108 fn read_bool(&mut self) -> Result<bool, E>;
109 fn read_f64(&mut self) -> Result<f64, E>;
110 fn read_f32(&mut self) -> Result<f32, E>;
111 fn read_char(&mut self) -> Result<char, E>;
112 fn read_str(&mut self) -> Result<String, E>;
115 fn read_enum<T>(&mut self, name: &str, f: |&mut Self| -> Result<T, E>) -> Result<T, E>;
117 fn read_enum_variant<T>(&mut self,
119 f: |&mut Self, uint| -> Result<T, E>)
121 fn read_enum_variant_arg<T>(&mut self,
123 f: |&mut Self| -> Result<T, E>)
126 fn read_enum_struct_variant<T>(&mut self,
128 f: |&mut Self, uint| -> Result<T, E>)
130 fn read_enum_struct_variant_field<T>(&mut self,
133 f: |&mut Self| -> Result<T, E>)
136 fn read_struct<T>(&mut self, s_name: &str, len: uint, f: |&mut Self| -> Result<T, E>)
138 fn read_struct_field<T>(&mut self,
141 f: |&mut Self| -> Result<T, E>)
144 fn read_tuple<T>(&mut self, f: |&mut Self, uint| -> Result<T, E>) -> Result<T, E>;
145 fn read_tuple_arg<T>(&mut self, a_idx: uint, f: |&mut Self| -> Result<T, E>) -> Result<T, E>;
147 fn read_tuple_struct<T>(&mut self,
149 f: |&mut Self, uint| -> Result<T, E>)
151 fn read_tuple_struct_arg<T>(&mut self,
153 f: |&mut Self| -> Result<T, E>)
156 // Specialized types:
157 fn read_option<T>(&mut self, f: |&mut Self, bool| -> Result<T, E>) -> Result<T, E>;
159 fn read_seq<T>(&mut self, f: |&mut Self, uint| -> Result<T, E>) -> Result<T, E>;
160 fn read_seq_elt<T>(&mut self, idx: uint, f: |&mut Self| -> Result<T, E>) -> Result<T, E>;
162 fn read_map<T>(&mut self, f: |&mut Self, uint| -> Result<T, E>) -> Result<T, E>;
163 fn read_map_elt_key<T>(&mut self, idx: uint, f: |&mut Self| -> Result<T, E>) -> Result<T, E>;
164 fn read_map_elt_val<T>(&mut self, idx: uint, f: |&mut Self| -> Result<T, E>) -> Result<T, E>;
167 fn error(&mut self, err: &str) -> E;
170 pub trait Encodable<S:Encoder<E>, E> {
171 fn encode(&self, s: &mut S) -> Result<(), E>;
174 pub trait Decodable<D:Decoder<E>, E> {
175 fn decode(d: &mut D) -> Result<Self, E>;
178 impl<E, S:Encoder<E>> Encodable<S, E> for uint {
179 fn encode(&self, s: &mut S) -> Result<(), E> {
184 impl<E, D:Decoder<E>> Decodable<D, E> for uint {
185 fn decode(d: &mut D) -> Result<uint, E> {
190 impl<E, S:Encoder<E>> Encodable<S, E> for u8 {
191 fn encode(&self, s: &mut S) -> Result<(), E> {
196 impl<E, D:Decoder<E>> Decodable<D, E> for u8 {
197 fn decode(d: &mut D) -> Result<u8, E> {
202 impl<E, S:Encoder<E>> Encodable<S, E> for u16 {
203 fn encode(&self, s: &mut S) -> Result<(), E> {
208 impl<E, D:Decoder<E>> Decodable<D, E> for u16 {
209 fn decode(d: &mut D) -> Result<u16, E> {
214 impl<E, S:Encoder<E>> Encodable<S, E> for u32 {
215 fn encode(&self, s: &mut S) -> Result<(), E> {
220 impl<E, D:Decoder<E>> Decodable<D, E> for u32 {
221 fn decode(d: &mut D) -> Result<u32, E> {
226 impl<E, S:Encoder<E>> Encodable<S, E> for u64 {
227 fn encode(&self, s: &mut S) -> Result<(), E> {
232 impl<E, D:Decoder<E>> Decodable<D, E> for u64 {
233 fn decode(d: &mut D) -> Result<u64, E> {
238 impl<E, S:Encoder<E>> Encodable<S, E> for int {
239 fn encode(&self, s: &mut S) -> Result<(), E> {
244 impl<E, D:Decoder<E>> Decodable<D, E> for int {
245 fn decode(d: &mut D) -> Result<int, E> {
250 impl<E, S:Encoder<E>> Encodable<S, E> for i8 {
251 fn encode(&self, s: &mut S) -> Result<(), E> {
256 impl<E, D:Decoder<E>> Decodable<D, E> for i8 {
257 fn decode(d: &mut D) -> Result<i8, E> {
262 impl<E, S:Encoder<E>> Encodable<S, E> for i16 {
263 fn encode(&self, s: &mut S) -> Result<(), E> {
268 impl<E, D:Decoder<E>> Decodable<D, E> for i16 {
269 fn decode(d: &mut D) -> Result<i16, E> {
274 impl<E, S:Encoder<E>> Encodable<S, E> for i32 {
275 fn encode(&self, s: &mut S) -> Result<(), E> {
280 impl<E, D:Decoder<E>> Decodable<D, E> for i32 {
281 fn decode(d: &mut D) -> Result<i32, E> {
286 impl<E, S:Encoder<E>> Encodable<S, E> for i64 {
287 fn encode(&self, s: &mut S) -> Result<(), E> {
292 impl<E, D:Decoder<E>> Decodable<D, E> for i64 {
293 fn decode(d: &mut D) -> Result<i64, E> {
298 impl<'a, E, S:Encoder<E>> Encodable<S, E> for &'a str {
299 fn encode(&self, s: &mut S) -> Result<(), E> {
304 impl<E, S:Encoder<E>> Encodable<S, E> for String {
305 fn encode(&self, s: &mut S) -> Result<(), E> {
306 s.emit_str(self.as_slice())
310 impl<E, D:Decoder<E>> Decodable<D, E> for String {
311 fn decode(d: &mut D) -> Result<String, E> {
312 Ok(String::from_str(try!(d.read_str()).as_slice()))
316 impl<E, S:Encoder<E>> Encodable<S, E> for f32 {
317 fn encode(&self, s: &mut S) -> Result<(), E> {
322 impl<E, D:Decoder<E>> Decodable<D, E> for f32 {
323 fn decode(d: &mut D) -> Result<f32, E> {
328 impl<E, S:Encoder<E>> Encodable<S, E> for f64 {
329 fn encode(&self, s: &mut S) -> Result<(), E> {
334 impl<E, D:Decoder<E>> Decodable<D, E> for f64 {
335 fn decode(d: &mut D) -> Result<f64, E> {
340 impl<E, S:Encoder<E>> Encodable<S, E> for bool {
341 fn encode(&self, s: &mut S) -> Result<(), E> {
346 impl<E, D:Decoder<E>> Decodable<D, E> for bool {
347 fn decode(d: &mut D) -> Result<bool, E> {
352 impl<E, S:Encoder<E>> Encodable<S, E> for char {
353 fn encode(&self, s: &mut S) -> Result<(), E> {
358 impl<E, D:Decoder<E>> Decodable<D, E> for char {
359 fn decode(d: &mut D) -> Result<char, E> {
364 impl<E, S:Encoder<E>> Encodable<S, E> for () {
365 fn encode(&self, s: &mut S) -> Result<(), E> {
370 impl<E, D:Decoder<E>> Decodable<D, E> for () {
371 fn decode(d: &mut D) -> Result<(), E> {
376 impl<'a, E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for &'a T {
377 fn encode(&self, s: &mut S) -> Result<(), E> {
382 impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for Box<T> {
383 fn encode(&self, s: &mut S) -> Result<(), E> {
388 impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for Box<T> {
389 fn decode(d: &mut D) -> Result<Box<T>, E> {
390 Ok(box try!(Decodable::decode(d)))
394 impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for Rc<T> {
396 fn encode(&self, s: &mut S) -> Result<(), E> {
401 impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for Rc<T> {
403 fn decode(d: &mut D) -> Result<Rc<T>, E> {
404 Ok(Rc::new(try!(Decodable::decode(d))))
408 impl<'a, E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for &'a [T] {
409 fn encode(&self, s: &mut S) -> Result<(), E> {
410 s.emit_seq(self.len(), |s| {
411 for (i, e) in self.iter().enumerate() {
412 try!(s.emit_seq_elt(i, |s| e.encode(s)))
419 impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for Vec<T> {
420 fn encode(&self, s: &mut S) -> Result<(), E> {
421 s.emit_seq(self.len(), |s| {
422 for (i, e) in self.iter().enumerate() {
423 try!(s.emit_seq_elt(i, |s| e.encode(s)))
430 impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for Vec<T> {
431 fn decode(d: &mut D) -> Result<Vec<T>, E> {
432 d.read_seq(|d, len| {
433 let mut v = Vec::with_capacity(len);
434 for i in range(0, len) {
435 v.push(try!(d.read_seq_elt(i, |d| Decodable::decode(d))));
442 impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for Option<T> {
443 fn encode(&self, s: &mut S) -> Result<(), E> {
446 None => s.emit_option_none(),
447 Some(ref v) => s.emit_option_some(|s| v.encode(s)),
453 impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for Option<T> {
454 fn decode(d: &mut D) -> Result<Option<T>, E> {
455 d.read_option(|d, b| {
457 Ok(Some(try!(Decodable::decode(d))))
465 macro_rules! peel(($name:ident, $($other:ident,)*) => (tuple!($($other,)*)))
469 ( $($name:ident,)+ ) => (
470 impl<E, D:Decoder<E>,$($name:Decodable<D, E>),*> Decodable<D,E> for ($($name,)*) {
471 #[allow(non_snake_case)]
472 fn decode(d: &mut D) -> Result<($($name,)*), E> {
473 d.read_tuple(|d, amt| {
475 let ret = ($(try!(d.read_tuple_arg({ i+=1; i-1 }, |d| -> Result<$name,E> {
479 "expected tuple of length `{}`, found tuple \
480 of length `{}`", i, amt);
485 impl<E, S:Encoder<E>,$($name:Encodable<S, E>),*> Encodable<S, E> for ($($name,)*) {
486 #[allow(non_snake_case)]
487 fn encode(&self, s: &mut S) -> Result<(), E> {
488 let ($(ref $name,)*) = *self;
490 $(let $name = $name; n += 1;)*
491 s.emit_tuple(n, |s| {
493 $(try!(s.emit_tuple_arg({ i+=1; i-1 }, |s| $name.encode(s)));)*
502 tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
504 impl<E, S: Encoder<E>> Encodable<S, E> for path::posix::Path {
505 fn encode(&self, e: &mut S) -> Result<(), E> {
506 self.as_vec().encode(e)
510 impl<E, D: Decoder<E>> Decodable<D, E> for path::posix::Path {
511 fn decode(d: &mut D) -> Result<path::posix::Path, E> {
512 let bytes: Vec<u8> = try!(Decodable::decode(d));
513 Ok(path::posix::Path::new(bytes))
517 impl<E, S: Encoder<E>> Encodable<S, E> for path::windows::Path {
518 fn encode(&self, e: &mut S) -> Result<(), E> {
519 self.as_vec().encode(e)
523 impl<E, D: Decoder<E>> Decodable<D, E> for path::windows::Path {
524 fn decode(d: &mut D) -> Result<path::windows::Path, E> {
525 let bytes: Vec<u8> = try!(Decodable::decode(d));
526 Ok(path::windows::Path::new(bytes))
530 impl<E, S: Encoder<E>, T: Encodable<S, E> + Copy> Encodable<S, E> for Cell<T> {
531 fn encode(&self, s: &mut S) -> Result<(), E> {
536 impl<E, D: Decoder<E>, T: Decodable<D, E> + Copy> Decodable<D, E> for Cell<T> {
537 fn decode(d: &mut D) -> Result<Cell<T>, E> {
538 Ok(Cell::new(try!(Decodable::decode(d))))
543 // Should use `try_borrow`, returning a
544 // `encoder.error("attempting to Encode borrowed RefCell")`
545 // from `encode` when `try_borrow` returns `None`.
547 impl<E, S: Encoder<E>, T: Encodable<S, E>> Encodable<S, E> for RefCell<T> {
548 fn encode(&self, s: &mut S) -> Result<(), E> {
549 self.borrow().encode(s)
553 impl<E, D: Decoder<E>, T: Decodable<D, E>> Decodable<D, E> for RefCell<T> {
554 fn decode(d: &mut D) -> Result<RefCell<T>, E> {
555 Ok(RefCell::new(try!(Decodable::decode(d))))
559 // ___________________________________________________________________________
562 pub trait EncoderHelpers<E> {
563 fn emit_from_vec<T>(&mut self,
565 f: |&mut Self, v: &T| -> Result<(), E>) -> Result<(), E>;
568 impl<E, S:Encoder<E>> EncoderHelpers<E> for S {
569 fn emit_from_vec<T>(&mut self, v: &[T], f: |&mut S, &T| -> Result<(), E>) -> Result<(), E> {
570 self.emit_seq(v.len(), |this| {
571 for (i, e) in v.iter().enumerate() {
572 try!(this.emit_seq_elt(i, |this| {
581 pub trait DecoderHelpers<E> {
582 fn read_to_vec<T>(&mut self, f: |&mut Self| -> Result<T, E>) -> Result<Vec<T>, E>;
585 impl<E, D:Decoder<E>> DecoderHelpers<E> for D {
586 fn read_to_vec<T>(&mut self, f: |&mut D| -> Result<T, E>) -> Result<Vec<T>, E> {
587 self.read_seq(|this, len| {
588 let mut v = Vec::with_capacity(len);
589 for i in range(0, len) {
590 v.push(try!(this.read_seq_elt(i, |this| f(this))));