From ab5e8780abe213a4d9fd4915aa87d7662046b647 Mon Sep 17 00:00:00 2001 From: Alexis Beingessner Date: Mon, 13 Nov 2017 13:37:36 -0500 Subject: [PATCH] Add deserialize_from to Deserialize --- serde/src/de/mod.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index 848183c7..aa1cc3af 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -504,6 +504,35 @@ pub trait Deserialize<'de>: Sized { fn deserialize(deserializer: D) -> Result 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`. + /// + /// TODO: example + /// + /// ``` + /// // Something with a loop that returns on error. + /// + /// ``` + fn deserialize_from(&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