serde icon indicating copy to clipboard operation
serde copied to clipboard

de: Make flattened structs and aliases interact correctly.

Open emilio opened this issue 6 years ago • 8 comments

The issue is that FlatStructAccess has no access to the aliases of the struct it's deserializing.

Ideally we'd try to serialize the key rather than checking whether we're going to use it before-hand, then actually take the value out, but that happens to be tricky with the current seed API.

So we need to somehow get the aliased names back to FlatStructAccess. Introduce a serialize_struct-like API that takes them in a backwards-compatible way. For parallelism, and since we also support aliases on enum variants, also extend the struct_variant API in a similar way. I'm open to better ways to fix it, but I can't think of any other that isn't a breaking change...

Fixes #1504.

emilio avatar May 04 '19 20:05 emilio

r? @dtolnay

emilio avatar May 04 '19 20:05 emilio

That's sort-of a breaking change, plus other serialize_struct implementations that use the fields use it as a hint to pre-allocate storage, which in the case of aliases may vastly over-allocate.

emilio avatar May 04 '19 21:05 emilio

Ah, I haven't heard of Deserializer impls pre-allocating storage based on deserialize_struct. Do you have an example of a format like that? Ordinarily it would be the other way around; the Deserializer impl is aware how much data is in the input, and the Deserialize impl needs to pre-allocate based on MapAccess::size_hint.

dtolnay avatar May 04 '19 21:05 dtolnay

Blerg, I was misreading code (was reading Serializer impls indeed).

But searched for a bit and found bits that would look at best fishy with that change. For example bincode serializes structs as tuples, and takes fields.len():

  • https://searchfox.org/mozilla-central/rev/06578bfadb5bdc5faee81f7e9b3c3fed21e616e0/third_party/rust/bincode/src/de/mod.rs#361

I haven't dug about whether this would break bincode for sure. I'm happy to dig out a bit. If it's just lying about a too large size hint it may be ok I guess....

emilio avatar May 04 '19 22:05 emilio

Is there any change to the status of this PR? I just ran into this issue in one of my projects, and it would be nice to have it fixed assuming this implementation covers all bases

dmarcuse avatar May 18 '19 03:05 dmarcuse

Same here. It's August and no movement. @dtolnay @emilio can this conversation be resuscitated or should we take up the mantle?

jnicholls avatar Aug 23 '19 21:08 jnicholls

Ping @dtolnay

jplatte avatar Dec 08 '20 14:12 jplatte

Ran into this while trying to make field name changes in a backward-compatible way.

KeyboardDanni avatar Dec 16 '20 23:12 KeyboardDanni