Example of IntoDeserializer

This commit is contained in:
David Tolnay 2017-04-14 14:24:47 -07:00
parent 9cda4563c0
commit c70c364754
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 59 additions and 4 deletions

View File

@ -1799,9 +1799,36 @@ pub trait VariantAccess<'de>: Sized {
////////////////////////////////////////////////////////////////////////////////
/// This trait converts primitive types into a deserializer.
/// Converts an existing value into a `Deserializer` from which other values can
/// be deserialized.
///
/// ```rust
/// #[macro_use]
/// extern crate serde_derive;
///
/// extern crate serde;
///
/// use std::str::FromStr;
/// use serde::de::{value, Deserialize, IntoDeserializer};
///
/// #[derive(Deserialize)]
/// enum Setting {
/// On,
/// Off,
/// }
///
/// impl FromStr for Setting {
/// type Err = value::Error;
///
/// fn from_str(s: &str) -> Result<Self, Self::Err> {
/// Self::deserialize(s.into_deserializer())
/// }
/// }
/// #
/// # fn main() {}
/// ```
pub trait IntoDeserializer<'de, E: Error = value::Error> {
/// The actual deserializer type.
/// The type of the deserializer being converted into.
type Deserializer: Deserializer<'de, Error = E>;
/// Convert this value into a deserializer.

View File

@ -6,7 +6,34 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! This module supports deserializing from primitives with the `ValueDeserializer` trait.
//! Building blocks for deserializing basic values using the `IntoDeserializer`
//! trait.
//!
//! ```rust
//! #[macro_use]
//! extern crate serde_derive;
//!
//! extern crate serde;
//!
//! use std::str::FromStr;
//! use serde::de::{value, Deserialize, IntoDeserializer};
//!
//! #[derive(Deserialize)]
//! enum Setting {
//! On,
//! Off,
//! }
//!
//! impl FromStr for Setting {
//! type Err = value::Error;
//!
//! fn from_str(s: &str) -> Result<Self, Self::Err> {
//! Self::deserialize(s.into_deserializer())
//! }
//! }
//! #
//! # fn main() {}
//! ```
use lib::*;
@ -16,7 +43,8 @@ use self::private::{First, Second};
////////////////////////////////////////////////////////////////////////////////
/// This represents all the possible errors that can occur using the `ValueDeserializer`.
/// A minimal representation of all possible errors that can occur using the
/// `IntoDeserializer` trait.
#[derive(Clone, Debug, PartialEq)]
pub struct Error {
err: ErrorImpl,