DeserializeSeed

Trait DeserializeSeed 

Source
pub trait DeserializeSeed<'de>: Sized {
    type Value;

    // Required method
    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
       where D: Deserializer<'de>;
}
Expand description

DeserializeSeed is the stateful form of the Deserialize trait. If you ever find yourself looking for a way to pass data into a Deserialize impl, this trait is the way to do it.

As one example of stateful deserialization consider deserializing a JSON array into an existing buffer. Using the Deserialize trait we could deserialize a JSON array into a Vec<T> but it would be a freshly allocated Vec<T>; there is no way for Deserialize to reuse a previously allocated buffer. Using DeserializeSeed instead makes this possible as in the example code below.

In practice the majority of deserialization is stateless. An API expecting a seed can be appeased by passing std::marker::PhantomData as a seed in the case of stateless deserialization.

§Lifetime

The 'de lifetime of this trait is the lifetime of data that may be borrowed by Self::Value when deserialized. See the page Understanding deserializer lifetimes for a more detailed explanation of these lifetimes.

Required Associated Types§

Source

type Value

The type produced by using this seed.

Required Methods§

Source

fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer<'de>,

Equivalent to the more common Deserialize::deserialize method, except with some initial piece of data (the seed) passed in.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'de, T> DeserializeSeed<'de> for PhantomData<T>
where T: Deserialize<'de>,

Source§

type Value = T

Source§

fn deserialize<D>(self, deserializer: D) -> Result<T, D::Error>
where D: Deserializer<'de>,

Implementors§