Add deserialize_from to Deserialize

This commit is contained in:
Alexis Beingessner
2017-11-13 13:37:36 -05:00
parent 0c34e06e51
commit ab5e8780ab
+29
View File
@@ -504,6 +504,35 @@ 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`.
///
/// TODO: example
///
/// ```
/// // Something with a loop that returns on error.
///
/// ```
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