mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-24 05:21:05 +00:00
Add deserialize_from to Deserialize
This commit is contained in:
@@ -504,6 +504,35 @@ pub trait Deserialize<'de>: Sized {
|
|||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where
|
where
|
||||||
D: Deserializer<'de>;
|
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
|
/// A data structure that can be deserialized without borrowing any data from
|
||||||
|
|||||||
Reference in New Issue
Block a user