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};
22 pub trait Encoder<E> {
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>;
42 fn emit_enum<F>(&mut self, name: &str, f: F) -> Result<(), E> where
43 F: FnOnce(&mut Self) -> Result<(), E>;
45 fn emit_enum_variant<F>(&mut self, v_name: &str,
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>;
53 fn emit_enum_struct_variant<F>(&mut self, v_name: &str,
56 f: F) -> Result<(), E> where
57 F: FnOnce(&mut Self) -> Result<(), E>;
58 fn emit_enum_struct_variant_field<F>(&mut self,
61 f: F) -> Result<(), E> where
62 F: FnOnce(&mut Self) -> Result<(), E>;
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>;
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>;
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>;
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>;
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>;
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>;
99 pub trait Decoder<E> {
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>;
119 fn read_enum<T, F>(&mut self, name: &str, f: F) -> Result<T, E> where
120 F: FnOnce(&mut Self) -> Result<T, E>;
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>;
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,
133 -> Result<T, E> where
134 F: FnOnce(&mut Self) -> Result<T, E>;
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,
142 -> Result<T, E> where
143 F: FnOnce(&mut Self) -> Result<T, E>;
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>;
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>;
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>;
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>;
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>;
172 fn error(&mut self, err: &str) -> E;
175 pub trait Encodable<S:Encoder<E>, E> for Sized? {
176 fn encode(&self, s: &mut S) -> Result<(), E>;
179 pub trait Decodable<D:Decoder<E>, E> {
180 fn decode(d: &mut D) -> Result<Self, E>;
183 impl<E, S:Encoder<E>> Encodable<S, E> for uint {
184 fn encode(&self, s: &mut S) -> Result<(), E> {
189 impl<E, D:Decoder<E>> Decodable<D, E> for uint {
190 fn decode(d: &mut D) -> Result<uint, E> {
195 impl<E, S:Encoder<E>> Encodable<S, E> for u8 {
196 fn encode(&self, s: &mut S) -> Result<(), E> {
201 impl<E, D:Decoder<E>> Decodable<D, E> for u8 {
202 fn decode(d: &mut D) -> Result<u8, E> {
207 impl<E, S:Encoder<E>> Encodable<S, E> for u16 {
208 fn encode(&self, s: &mut S) -> Result<(), E> {
213 impl<E, D:Decoder<E>> Decodable<D, E> for u16 {
214 fn decode(d: &mut D) -> Result<u16, E> {
219 impl<E, S:Encoder<E>> Encodable<S, E> for u32 {
220 fn encode(&self, s: &mut S) -> Result<(), E> {
225 impl<E, D:Decoder<E>> Decodable<D, E> for u32 {
226 fn decode(d: &mut D) -> Result<u32, E> {
231 impl<E, S:Encoder<E>> Encodable<S, E> for u64 {
232 fn encode(&self, s: &mut S) -> Result<(), E> {
237 impl<E, D:Decoder<E>> Decodable<D, E> for u64 {
238 fn decode(d: &mut D) -> Result<u64, E> {
243 impl<E, S:Encoder<E>> Encodable<S, E> for int {
244 fn encode(&self, s: &mut S) -> Result<(), E> {
249 impl<E, D:Decoder<E>> Decodable<D, E> for int {
250 fn decode(d: &mut D) -> Result<int, E> {
255 impl<E, S:Encoder<E>> Encodable<S, E> for i8 {
256 fn encode(&self, s: &mut S) -> Result<(), E> {
261 impl<E, D:Decoder<E>> Decodable<D, E> for i8 {
262 fn decode(d: &mut D) -> Result<i8, E> {
267 impl<E, S:Encoder<E>> Encodable<S, E> for i16 {
268 fn encode(&self, s: &mut S) -> Result<(), E> {
273 impl<E, D:Decoder<E>> Decodable<D, E> for i16 {
274 fn decode(d: &mut D) -> Result<i16, E> {
279 impl<E, S:Encoder<E>> Encodable<S, E> for i32 {
280 fn encode(&self, s: &mut S) -> Result<(), E> {
285 impl<E, D:Decoder<E>> Decodable<D, E> for i32 {
286 fn decode(d: &mut D) -> Result<i32, E> {
291 impl<E, S:Encoder<E>> Encodable<S, E> for i64 {
292 fn encode(&self, s: &mut S) -> Result<(), E> {
297 impl<E, D:Decoder<E>> Decodable<D, E> for i64 {
298 fn decode(d: &mut D) -> Result<i64, E> {
303 impl<E, S:Encoder<E>> Encodable<S, E> for str {
304 fn encode(&self, s: &mut S) -> Result<(), E> {
309 impl<E, S:Encoder<E>> Encodable<S, E> for String {
310 fn encode(&self, s: &mut S) -> Result<(), E> {
315 impl<E, D:Decoder<E>> Decodable<D, E> for String {
316 fn decode(d: &mut D) -> Result<String, E> {
321 impl<E, S:Encoder<E>> Encodable<S, E> for f32 {
322 fn encode(&self, s: &mut S) -> Result<(), E> {
327 impl<E, D:Decoder<E>> Decodable<D, E> for f32 {
328 fn decode(d: &mut D) -> Result<f32, E> {
333 impl<E, S:Encoder<E>> Encodable<S, E> for f64 {
334 fn encode(&self, s: &mut S) -> Result<(), E> {
339 impl<E, D:Decoder<E>> Decodable<D, E> for f64 {
340 fn decode(d: &mut D) -> Result<f64, E> {
345 impl<E, S:Encoder<E>> Encodable<S, E> for bool {
346 fn encode(&self, s: &mut S) -> Result<(), E> {
351 impl<E, D:Decoder<E>> Decodable<D, E> for bool {
352 fn decode(d: &mut D) -> Result<bool, E> {
357 impl<E, S:Encoder<E>> Encodable<S, E> for char {
358 fn encode(&self, s: &mut S) -> Result<(), E> {
363 impl<E, D:Decoder<E>> Decodable<D, E> for char {
364 fn decode(d: &mut D) -> Result<char, E> {
369 impl<E, S:Encoder<E>> Encodable<S, E> for () {
370 fn encode(&self, s: &mut S) -> Result<(), E> {
375 impl<E, D:Decoder<E>> Decodable<D, E> for () {
376 fn decode(d: &mut D) -> Result<(), E> {
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> {
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> {
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)))
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())
406 impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for Rc<T> {
408 fn encode(&self, s: &mut S) -> Result<(), E> {
413 impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for Rc<T> {
415 fn decode(d: &mut D) -> Result<Rc<T>, E> {
416 Ok(Rc::new(try!(Decodable::decode(d))))
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)))
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)))
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))));
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> {
458 None => s.emit_option_none(),
459 Some(ref v) => s.emit_option_some(|s| v.encode(s)),
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| {
469 Ok(Some(try!(Decodable::decode(d))))
478 ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
481 /// Evaluates to the number of identifiers passed to it, for example: `count_idents!(a, b, c) == 3
482 macro_rules! count_idents {
484 ($_i:ident $(, $rest:ident)*) => { 1 + count_idents!($($rest),*) }
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| {
496 let ret = ($(try!(d.read_tuple_arg({ i+=1; i-1 }, |d| -> Result<$name,E> {
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;
508 $(let $name = $name; n += 1;)*
509 s.emit_tuple(n, |s| {
511 $(try!(s.emit_tuple_arg({ i+=1; i-1 }, |s| $name.encode(s)));)*
520 tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
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)
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))
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)
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))
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> {
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))))
561 // Should use `try_borrow`, returning a
562 // `encoder.error("attempting to Encode borrowed RefCell")`
563 // from `encode` when `try_borrow` returns `None`.
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)
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))))
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> {
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))))
589 // ___________________________________________________________________________
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>;
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>,
601 self.emit_seq(v.len(), |this| {
602 for (i, e) in v.iter().enumerate() {
603 try!(this.emit_seq_elt(i, |this| {
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>;
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>,
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))));