Serde: I got lost between MapAccess, DeserializeSeed and PhantomData

I try to implement my own Serde deserializer for my non-default parsing problem. It is an educational project to sharpen my Rust skill, which means that I have real world use case, but my focus is on really understanding what's going on.

I'll try to point out where I get lost in the Serde code:

I have an example struct that derives Deserialize. I use cargo expand to look at the generated code and find the following call:

__field0 = _serde::__private228::Some(
    _serde::de::MapAccess::next_value::<Handle<AssetA>,>(&mut __map)?
);

I watched this Video by @jonhoo and think that I have decent understanding up until here. My own Deserializer provides its own struct that implements MapAccess and the required next_key_seed and next_key_value methods.

I struggle to understand where the seed is coming from. Trying to read the Serde source code I ended up at the default implementation of next_value:

    #[inline]
    fn next_value<V>(&mut self) -> Result<V, Self::Error>
    where
        V: Deserialize<'de>,
    {
        self.next_value_seed(PhantomData)
    }

The docs of DeserializeSeed say

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

so I expected some struct holding some data, which to my understanding is quite the opposite of PhantomData!? Looking at the source code of DeserializeSeed I found that is implemented for PhantomData:

impl<'de, T> DeserializeSeed<'de> for PhantomData<T>
where
    T: Deserialize<'de>,
{
    type Value = T;

    #[inline]
    fn deserialize<D>(self, deserializer: D) -> Result<T, D::Error>
    where
        D: Deserializer<'de>,
    {
        T::deserialize(deserializer)
    }
}

If I get the connection right, the T in the above code would be my Handle<AssetA> from the generated next_value call. So all of this seems to boil down to a call to deserialize of the type argument of next_value.

Is that correct? Can somebody explain the reasoning behind this to me? Up until now I encountered PhantomData only as field type. This code seems to rely on an "instance" of PhantomData which is a new challenge to wrap my head around. :wink:

Any enlightening comments would be highly appreciated!

as a Deserializer -- a parser for a custom format, the seed is passed to you by the visitor as a (generic) argument. you don't need to worry about it. you just forward your next_value_seed() to its deserialize() method with a parser for the next data item as the argument.

ultimately, it is up to the user who uses stateful deserializable data types to create the correct initial states to initiate (i.e. to "seed") the (stateful) deserialization process.

MapAccess and friends require the implementor to support any kind of stateful (seeded) deserialization by providing next_key_seed<K> and next_value_seed<K> for all K: DeserializeSeed<'de>. (You're expected to use the seed.)

The API docs have an example (using the analogous SeqAccess::next_element_seed) of utilizing that support, wherein a visitor supplies the seed. I think it demonstrates the motivation of DeserializeSeed and the *_seed methods well.

PhantomData<T>'s implementation of DeserializeSeed corresponds deserializing T without actually having seed data, so it's exactly the same as <T as Deserialize>::deserialize in that case (which is what the implementation shows).

Therefore next_key_seed and next_value_seed[1] also support stateless deserialization -- that's what you effectively request if you call them with K = PhantomData<_>. And that is what next_key and next_value do.


  1. and next_element_seed ↩︎