Skip to content

Commit 9416935

Browse files
committed
Improve examples for syntax::ext::deriving::encodable
The examples in the documentation for syntax::ext::deriving::encodable are outdated, and do not work. To fix this, the following changes are applied: - emit_field() -> emit_struct_field() - read_field() -> read_struct_field() - Use Result to report errors - Add the mut keyword to Encoder/Decoder - Prefer Encodable::encode() to emit_uint
1 parent bb2168c commit 9416935

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

src/libsyntax/ext/deriving/encodable.rs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@
2222
//! would generate two implementations like:
2323
//!
2424
//! ```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+
//! })
2932
//! })
3033
//! }
3134
//! }
3235
//!
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),
3842
//! }
3943
//! })
4044
//! }
@@ -46,37 +50,42 @@
4650
//!
4751
//! ```ignore
4852
//! #[deriving(Encodable, Decodable)]
49-
//! struct spanned<T> { node: T, span: Span }
53+
//! struct Spanned<T> { node: T, span: Span }
5054
//! ```
5155
//!
5256
//! would yield functions like:
5357
//!
5458
//! ```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+
//! })
6570
//! }
71+
//! }
6672
//!
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(),
7785
//! })
78-
//! }
86+
//! })
7987
//! }
88+
//! }
8089
//! ```
8190
8291
use ast::{MetaItem, Item, Expr, ExprRet, MutMutable, LitNil};

0 commit comments

Comments
 (0)