From 385f83a7dec7f34368c013b8efcf2b2f490d50a9 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 13 Apr 2017 15:16:43 -0700 Subject: [PATCH] Examples and links for Option --- serde/src/ser/mod.rs | 64 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/serde/src/ser/mod.rs b/serde/src/ser/mod.rs index f72eba4d..1eaae172 100644 --- a/serde/src/ser/mod.rs +++ b/serde/src/ser/mod.rs @@ -703,10 +703,70 @@ pub trait Serializer: Sized { /// ``` fn serialize_bytes(self, value: &[u8]) -> Result; - /// Serialize a `None` value. + /// Serialize a [`None`] value. + /// + /// ```rust + /// # extern crate serde; + /// # + /// # use serde::{Serialize, Serializer}; + /// # + /// # enum Option { + /// # Some(T), + /// # None, + /// # } + /// # + /// # use Option::{Some, None}; + /// # + /// impl Serialize for Option + /// where T: Serialize + /// { + /// fn serialize(&self, serializer: S) -> Result + /// where S: Serializer + /// { + /// match *self { + /// Some(ref value) => serializer.serialize_some(value), + /// None => serializer.serialize_none(), + /// } + /// } + /// } + /// # + /// # fn main() {} + /// ``` + /// + /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None fn serialize_none(self) -> Result; - /// Serialize a `Some(T)` value. + /// Serialize a [`Some(T)`] value. + /// + /// ```rust + /// # extern crate serde; + /// # + /// # use serde::{Serialize, Serializer}; + /// # + /// # enum Option { + /// # Some(T), + /// # None, + /// # } + /// # + /// # use Option::{Some, None}; + /// # + /// impl Serialize for Option + /// where T: Serialize + /// { + /// fn serialize(&self, serializer: S) -> Result + /// where S: Serializer + /// { + /// match *self { + /// Some(ref value) => serializer.serialize_some(value), + /// None => serializer.serialize_none(), + /// } + /// } + /// } + /// # + /// # fn main() {} + /// ``` + /// + /// [`Some(T)`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some fn serialize_some(self, value: &T) -> Result where T: Serialize;