From 94169353eca74c3683b06cea2609a4350ed36c45 Mon Sep 17 00:00:00 2001 From: Barosl Lee Date: Fri, 14 Nov 2014 17:14:44 +0900 Subject: [PATCH] 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 --- src/libsyntax/ext/deriving/encodable.rs | 71 ++++++++++++++----------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/src/libsyntax/ext/deriving/encodable.rs b/src/libsyntax/ext/deriving/encodable.rs index 103253560df..69eb260b8c4 100644 --- a/src/libsyntax/ext/deriving/encodable.rs +++ b/src/libsyntax/ext/deriving/encodable.rs @@ -22,19 +22,23 @@ //! would generate two implementations like: //! //! ```ignore -//! impl Encodable for Node { -//! fn encode(&self, s: &S) { -//! s.emit_struct("Node", 1, || { -//! s.emit_field("id", 0, || s.emit_uint(self.id)) +//! impl, E> Encodable for Node { +//! fn encode(&self, s: &mut S) -> Result<(), E> { +//! s.emit_struct("Node", 1, |this| { +//! this.emit_struct_field("id", 0, |this| { +//! Encodable::encode(&self.id, this) +//! /* this.emit_uint(self.id) can also be used */ +//! }) //! }) //! } //! } //! -//! impl Decodable for node_id { -//! fn decode(d: &D) -> Node { -//! d.read_struct("Node", 1, || { -//! Node { -//! id: d.read_field("x".to_string(), 0, || decode(d)) +//! impl, E> Decodable for Node { +//! fn decode(d: &mut D) -> Result { +//! d.read_struct("Node", 1, |this| { +//! match this.read_struct_field("id", 0, |this| Decodable::decode(this)) { +//! Ok(id) => Ok(Node { id: id }), +//! Err(e) => Err(e), //! } //! }) //! } @@ -46,37 +50,42 @@ //! //! ```ignore //! #[deriving(Encodable, Decodable)] -//! struct spanned { node: T, span: Span } +//! struct Spanned { node: T, span: Span } //! ``` //! //! would yield functions like: //! //! ```ignore -//! impl< -//! S: Encoder, -//! T: Encodable -//! > spanned: Encodable { -//! fn encode(s: &S) { -//! s.emit_rec(|| { -//! s.emit_field("node", 0, || self.node.encode(s)); -//! s.emit_field("span", 1, || self.span.encode(s)); -//! }) -//! } +//! impl< +//! S: Encoder, +//! E, +//! T: Encodable +//! > Encodable for Spanned { +//! fn encode(&self, s: &mut S) -> Result<(), E> { +//! s.emit_struct("Spanned", 2, |this| { +//! this.emit_struct_field("node", 0, |this| self.node.encode(this)) +//! .ok().unwrap(); +//! this.emit_struct_field("span", 1, |this| self.span.encode(this)) +//! }) //! } +//! } //! -//! impl< -//! D: Decoder, -//! T: Decodable -//! > spanned: Decodable { -//! fn decode(d: &D) -> spanned { -//! d.read_rec(|| { -//! { -//! node: d.read_field("node".to_string(), 0, || decode(d)), -//! span: d.read_field("span".to_string(), 1, || decode(d)), -//! } +//! impl< +//! D: Decoder, +//! E, +//! T: Decodable +//! > Decodable for Spanned { +//! fn decode(d: &mut D) -> Result, E> { +//! d.read_struct("Spanned", 2, |this| { +//! Ok(Spanned { +//! node: this.read_struct_field("node", 0, |this| Decodable::decode(this)) +//! .ok().unwrap(), +//! span: this.read_struct_field("span", 1, |this| Decodable::decode(this)) +//! .ok().unwrap(), //! }) -//! } +//! }) //! } +//! } //! ``` use ast::{MetaItem, Item, Expr, ExprRet, MutMutable, LitNil}; -- 2.44.0