Make it possible to offer roundtrip guarantees for formats that need it #2778
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
Serializer
andDeserializer
traits have a nice symmetry in their methods. In theory, that means it should be possible to write serialisers and deserialisers that offer round trip guarantees across its entire data model. In practice this is not possible, however, because theVisitor
trait does not have a one to one translation with deserialisers. Unlike deserialisers, for example, it makes no distinction between structs and maps.This PR adds additional methods to the Visitor trait to ensure that symmetry is kept. It also adds default implementations that maintain the current behaviour to ensure backwards compatibility. This allows formats that need it to offer round trip guarantees across the entire Serde data model without affecting existing formats. This behaviour is tested in this PR.
Edit: To give some context, I'm aware of this previous issue which actually advocated for removing
Visitor::visit_unit_struct
(one of the methods this PR actually adds). We need this method along with the other struct, tuple and enum related methods this PR is adding becauseserde_content::Value
is defined asWithout the methods this PR is adding,
ValueVisitor
wouldn't have enough context to be able to support the struct, tuple and enum variants. Being able to distinguish between structs and maps and the specific types of structs and enums allows us to return more informative errors. For example, trying to deserialiseinto a unit would return something like this