about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorJakub Bukaj <jakub@jakub.cc>2014-11-16 10:20:28 +0100
committerJakub Bukaj <jakub@jakub.cc>2014-11-16 10:20:28 +0100
commit12144098eb0946fa925f7a0fbf52b2e4a701bf25 (patch)
tree63a4e1cbfa86f1d8faec884a4fdac7a6a830b24e /src/libsyntax
parentb45dbfbd2b6835dffe07e87846f8349ff55927ce (diff)
parent94169353eca74c3683b06cea2609a4350ed36c45 (diff)
downloadrust-12144098eb0946fa925f7a0fbf52b2e4a701bf25.tar.gz
rust-12144098eb0946fa925f7a0fbf52b2e4a701bf25.zip
rollup merge of #18948: barosl/doc-encodable-fix
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ext/deriving/encodable.rs71
1 files 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<S:serialize::Encoder> Encodable<S> for Node {
-//!     fn encode(&self, s: &S) {
-//!         s.emit_struct("Node", 1, || {
-//!             s.emit_field("id", 0, || s.emit_uint(self.id))
+//! impl<S: Encoder<E>, E> Encodable<S, E> 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<D:Decoder> 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<D: Decoder<E>, E> Decodable<D, E> for Node {
+//!     fn decode(d: &mut D) -> Result<Node, E> {
+//!         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<T> { node: T, span: Span }
+//! struct Spanned<T> { node: T, span: Span }
 //! ```
 //!
 //! would yield functions like:
 //!
 //! ```ignore
-//!     impl<
-//!         S: Encoder,
-//!         T: Encodable<S>
-//!     > spanned<T>: Encodable<S> {
-//!         fn encode<S:Encoder>(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>,
+//!     E,
+//!     T: Encodable<S, E>
+//! > Encodable<S, E> for Spanned<T> {
+//!     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<D>
-//!     > spanned<T>: Decodable<D> {
-//!         fn decode(d: &D) -> spanned<T> {
-//!             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>,
+//!     E,
+//!     T: Decodable<D, E>
+//! > Decodable<D, E> for Spanned<T> {
+//!     fn decode(d: &mut D) -> Result<Spanned<T>, 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};