|
22 | 22 | //! would generate two implementations like:
|
23 | 23 | //!
|
24 | 24 | //! ```ignore
|
25 |
| -//! impl<S:serialize::Encoder> Encodable<S> for Node { |
26 |
| -//! fn encode(&self, s: &S) { |
27 |
| -//! s.emit_struct("Node", 1, || { |
28 |
| -//! s.emit_field("id", 0, || s.emit_uint(self.id)) |
| 25 | +//! impl<S: Encoder<E>, E> Encodable<S, E> for Node { |
| 26 | +//! fn encode(&self, s: &mut S) -> Result<(), E> { |
| 27 | +//! s.emit_struct("Node", 1, |this| { |
| 28 | +//! this.emit_struct_field("id", 0, |this| { |
| 29 | +//! Encodable::encode(&self.id, this) |
| 30 | +//! /* this.emit_uint(self.id) can also be used */ |
| 31 | +//! }) |
29 | 32 | //! })
|
30 | 33 | //! }
|
31 | 34 | //! }
|
32 | 35 | //!
|
33 |
| -//! impl<D:Decoder> Decodable for node_id { |
34 |
| -//! fn decode(d: &D) -> Node { |
35 |
| -//! d.read_struct("Node", 1, || { |
36 |
| -//! Node { |
37 |
| -//! id: d.read_field("x".to_string(), 0, || decode(d)) |
| 36 | +//! impl<D: Decoder<E>, E> Decodable<D, E> for Node { |
| 37 | +//! fn decode(d: &mut D) -> Result<Node, E> { |
| 38 | +//! d.read_struct("Node", 1, |this| { |
| 39 | +//! match this.read_struct_field("id", 0, |this| Decodable::decode(this)) { |
| 40 | +//! Ok(id) => Ok(Node { id: id }), |
| 41 | +//! Err(e) => Err(e), |
38 | 42 | //! }
|
39 | 43 | //! })
|
40 | 44 | //! }
|
|
46 | 50 | //!
|
47 | 51 | //! ```ignore
|
48 | 52 | //! #[deriving(Encodable, Decodable)]
|
49 |
| -//! struct spanned<T> { node: T, span: Span } |
| 53 | +//! struct Spanned<T> { node: T, span: Span } |
50 | 54 | //! ```
|
51 | 55 | //!
|
52 | 56 | //! would yield functions like:
|
53 | 57 | //!
|
54 | 58 | //! ```ignore
|
55 |
| -//! impl< |
56 |
| -//! S: Encoder, |
57 |
| -//! T: Encodable<S> |
58 |
| -//! > spanned<T>: Encodable<S> { |
59 |
| -//! fn encode<S:Encoder>(s: &S) { |
60 |
| -//! s.emit_rec(|| { |
61 |
| -//! s.emit_field("node", 0, || self.node.encode(s)); |
62 |
| -//! s.emit_field("span", 1, || self.span.encode(s)); |
63 |
| -//! }) |
64 |
| -//! } |
| 59 | +//! impl< |
| 60 | +//! S: Encoder<E>, |
| 61 | +//! E, |
| 62 | +//! T: Encodable<S, E> |
| 63 | +//! > Encodable<S, E> for Spanned<T> { |
| 64 | +//! fn encode(&self, s: &mut S) -> Result<(), E> { |
| 65 | +//! s.emit_struct("Spanned", 2, |this| { |
| 66 | +//! this.emit_struct_field("node", 0, |this| self.node.encode(this)) |
| 67 | +//! .ok().unwrap(); |
| 68 | +//! this.emit_struct_field("span", 1, |this| self.span.encode(this)) |
| 69 | +//! }) |
65 | 70 | //! }
|
| 71 | +//! } |
66 | 72 | //!
|
67 |
| -//! impl< |
68 |
| -//! D: Decoder, |
69 |
| -//! T: Decodable<D> |
70 |
| -//! > spanned<T>: Decodable<D> { |
71 |
| -//! fn decode(d: &D) -> spanned<T> { |
72 |
| -//! d.read_rec(|| { |
73 |
| -//! { |
74 |
| -//! node: d.read_field("node".to_string(), 0, || decode(d)), |
75 |
| -//! span: d.read_field("span".to_string(), 1, || decode(d)), |
76 |
| -//! } |
| 73 | +//! impl< |
| 74 | +//! D: Decoder<E>, |
| 75 | +//! E, |
| 76 | +//! T: Decodable<D, E> |
| 77 | +//! > Decodable<D, E> for Spanned<T> { |
| 78 | +//! fn decode(d: &mut D) -> Result<Spanned<T>, E> { |
| 79 | +//! d.read_struct("Spanned", 2, |this| { |
| 80 | +//! Ok(Spanned { |
| 81 | +//! node: this.read_struct_field("node", 0, |this| Decodable::decode(this)) |
| 82 | +//! .ok().unwrap(), |
| 83 | +//! span: this.read_struct_field("span", 1, |this| Decodable::decode(this)) |
| 84 | +//! .ok().unwrap(), |
77 | 85 | //! })
|
78 |
| -//! } |
| 86 | +//! }) |
79 | 87 | //! }
|
| 88 | +//! } |
80 | 89 | //! ```
|
81 | 90 |
|
82 | 91 | use ast::{MetaItem, Item, Expr, ExprRet, MutMutable, LitNil};
|
|
0 commit comments