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
This commit is contained in:
parent
bb2168c525
commit
94169353ec
1 changed files with 40 additions and 31 deletions
|
@ -22,19 +22,23 @@
|
||||||
//! would generate two implementations like:
|
//! would generate two implementations like:
|
||||||
//!
|
//!
|
||||||
//! ```ignore
|
//! ```ignore
|
||||||
//! impl<S:serialize::Encoder> Encodable<S> for Node {
|
//! impl<S: Encoder<E>, E> Encodable<S, E> for Node {
|
||||||
//! fn encode(&self, s: &S) {
|
//! fn encode(&self, s: &mut S) -> Result<(), E> {
|
||||||
//! s.emit_struct("Node", 1, || {
|
//! s.emit_struct("Node", 1, |this| {
|
||||||
//! s.emit_field("id", 0, || s.emit_uint(self.id))
|
//! this.emit_struct_field("id", 0, |this| {
|
||||||
|
//! Encodable::encode(&self.id, this)
|
||||||
|
//! /* this.emit_uint(self.id) can also be used */
|
||||||
|
//! })
|
||||||
//! })
|
//! })
|
||||||
//! }
|
//! }
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! impl<D:Decoder> Decodable for node_id {
|
//! impl<D: Decoder<E>, E> Decodable<D, E> for Node {
|
||||||
//! fn decode(d: &D) -> Node {
|
//! fn decode(d: &mut D) -> Result<Node, E> {
|
||||||
//! d.read_struct("Node", 1, || {
|
//! d.read_struct("Node", 1, |this| {
|
||||||
//! Node {
|
//! match this.read_struct_field("id", 0, |this| Decodable::decode(this)) {
|
||||||
//! id: d.read_field("x".to_string(), 0, || decode(d))
|
//! Ok(id) => Ok(Node { id: id }),
|
||||||
|
//! Err(e) => Err(e),
|
||||||
//! }
|
//! }
|
||||||
//! })
|
//! })
|
||||||
//! }
|
//! }
|
||||||
|
@ -46,37 +50,42 @@
|
||||||
//!
|
//!
|
||||||
//! ```ignore
|
//! ```ignore
|
||||||
//! #[deriving(Encodable, Decodable)]
|
//! #[deriving(Encodable, Decodable)]
|
||||||
//! struct spanned<T> { node: T, span: Span }
|
//! struct Spanned<T> { node: T, span: Span }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! would yield functions like:
|
//! would yield functions like:
|
||||||
//!
|
//!
|
||||||
//! ```ignore
|
//! ```ignore
|
||||||
//! impl<
|
//! impl<
|
||||||
//! S: Encoder,
|
//! S: Encoder<E>,
|
||||||
//! T: Encodable<S>
|
//! E,
|
||||||
//! > spanned<T>: Encodable<S> {
|
//! T: Encodable<S, E>
|
||||||
//! fn encode<S:Encoder>(s: &S) {
|
//! > Encodable<S, E> for Spanned<T> {
|
||||||
//! s.emit_rec(|| {
|
//! fn encode(&self, s: &mut S) -> Result<(), E> {
|
||||||
//! s.emit_field("node", 0, || self.node.encode(s));
|
//! s.emit_struct("Spanned", 2, |this| {
|
||||||
//! s.emit_field("span", 1, || self.span.encode(s));
|
//! 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<
|
//! impl<
|
||||||
//! D: Decoder,
|
//! D: Decoder<E>,
|
||||||
//! T: Decodable<D>
|
//! E,
|
||||||
//! > spanned<T>: Decodable<D> {
|
//! T: Decodable<D, E>
|
||||||
//! fn decode(d: &D) -> spanned<T> {
|
//! > Decodable<D, E> for Spanned<T> {
|
||||||
//! d.read_rec(|| {
|
//! fn decode(d: &mut D) -> Result<Spanned<T>, E> {
|
||||||
//! {
|
//! d.read_struct("Spanned", 2, |this| {
|
||||||
//! node: d.read_field("node".to_string(), 0, || decode(d)),
|
//! Ok(Spanned {
|
||||||
//! span: d.read_field("span".to_string(), 1, || decode(d)),
|
//! 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};
|
use ast::{MetaItem, Item, Expr, ExprRet, MutMutable, LitNil};
|
||||||
|
|
Loading…
Reference in a new issue