Skip to content

Commit

Permalink
Add deserialize_from to Deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
Gankra committed Nov 13, 2017
1 parent e198afb commit 3e435c0
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions serde/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3e435c0

Please sign in to comment.