Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and derive deserialize_from #1094

Merged
merged 18 commits into from
Dec 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
283 changes: 236 additions & 47 deletions serde/src/de/impls.rs

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions serde/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,34 @@ pub trait Deserialize<'de>: Sized {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>;

/// Deserializes a value into `self` from the given Deserializer.
///
/// The purpose of this method is to allow the deserializer to reuse
/// resources and avoid copies. As such, if this method returns an error,
/// `self` will be in an indeterminate state where some parts of the struct
/// have been overwritten. Although whatever state that is will be
/// memory-safe.
///
/// This is generally useful when repeateadly deserializing values that
/// are processed one at a time, where the value of `self` doesn't matter
/// when the next deserialization occurs.
///
/// If you manually implement this, your recursive deserializations should
/// use `deserialize_from`.
///
/// This method is stable and an official public API, but hidden from the
/// documentation because it is almost never what newbies are looking for.
/// Showing it in rustdoc would cause it to be featured more prominently
/// than it deserves.
#[doc(hidden)]
fn deserialize_from<D>(&mut self, deserializer: D) -> Result<(), D::Error>
where D: Deserializer<'de>
{
// Default implementation just delegates to `deserialize` impl.
*self = Deserialize::deserialize(deserializer)?;
Ok(())
}
}

/// A data structure that can be deserialized without borrowing any data from
Expand Down
19 changes: 18 additions & 1 deletion serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use lib::*;

use de::{Deserialize, Deserializer, IntoDeserializer, Error, Visitor};
use de::{Deserialize, Deserializer, DeserializeSeed, IntoDeserializer, Error, Visitor};

#[cfg(any(feature = "std", feature = "alloc"))]
use de::Unexpected;
Expand Down Expand Up @@ -2009,3 +2009,20 @@ where
map struct enum identifier ignored_any
}
}

/// A DeserializeSeed helper for implementing deserialize_from Visitors.
///
/// Wraps a mutable reference and calls deserialize_from on it.
pub struct DeserializeFromSeed<'a, T: 'a>(pub &'a mut T);

impl<'a, 'de, T> DeserializeSeed<'de> for DeserializeFromSeed<'a, T>
where T: Deserialize<'de>,
{
type Value = ();
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
self.0.deserialize_from(deserializer)
}
}
4 changes: 4 additions & 0 deletions serde_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-
[badges]
travis-ci = { repository = "serde-rs/serde" }

[features]
default = []
deserialize_from = []

[lib]
name = "serde_derive"
proc-macro = true
Expand Down
Loading