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::deserializemethod, 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. ![]()
Any enlightening comments would be highly appreciated!
1 post - 1 participant
