You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implements a postcard-dyn "reserializer" that deserializes data from an arbitrary Deserializer and feeds it into an arbitrary Serializer in a streaming fashion (without needing to construct an intermediate representation in memory) based on a postcard schema. This means postcard -> serde_json::Value (or whatever generic representation postcard-dyn ends up providing), postcard <-> json, and arbitrary other postcard <-> X transformations can be performed by the same generic implementation.
Unfortunately, Deserializer and Serializer methods require &'static strs for structs/variants/field names. To provide these, the lossless implementation allocates and leaks one copy of each unique string that must be used as &'static. I can't find a way around this that isn't lossy and this seems inevitable in order to provide a Serializeable postcard_dyn::Value. A separate, lossy transformation is also provided that does not leak memory but only supports json-like representations of structs/enums as maps.
Ouch, yeah, that first limitiation. I don't really love that in the "always" case, do you think this would make sense as an alternate interface in this crate instead of replacing the existing machinery?
Ouch, yeah, that first limitiation. I don't really love that in the "always" case, do you think this would make sense as an alternate interface in this crate instead of replacing the existing machinery?
Yes, probably, and it should be doable to provide both interfaces backed by a single implementation with a parameter selecting either "lossy, non-leaky transformation" or "lossless, leaky" transformation.
However, what do we expect users to do with a Value that doesn't involve Serializeing it in the end (which runs into this limitation)? I'm worried that most transformations on the generic form will end up running into this limitation as well. With the existing interface it isn't a problem because serde_json converts structs and enums to maps, but I'm assuming that isn't the end-goal.
One other option: we could expose an unsafe interface for serializers that are known not to keep struct/field/variant names around for longer than the serializer lives (I believe this is the case for most serializers) that transmutes the lifetimes of those strings to 'static during serialization instead of leaking them.
Pushed an update that avoids leaking for the from_slice_dyn() case and makes it easier to implement different workarounds for the &'static str limitation. Lots of boilerplate but hopefully reasonably straightforward
Some more context on the &'static str limitation and what it may take to lift it in serde itself (TLDR: unlikely to ever happen): serde-rs/serde#2218 (comment)
max-heller
changed the title
postcard-dyn: reserialize into arbitrary Serializers
postcard-dyn: reserialize from arbitrary Deserializer into arbitrary Serializer
Dec 16, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
postcard-schemaRelated to the postcard-schema crateQ2 '25 TriageItems tracked as part of https://github.com/jamesmunns/postcard/issues/241
2 participants
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.
Implements a
postcard-dyn"reserializer" that deserializes data from an arbitraryDeserializerand feeds it into an arbitrarySerializerin a streaming fashion (without needing to construct an intermediate representation in memory) based on a postcard schema. This meanspostcard -> serde_json::Value(or whatever generic representationpostcard-dynends up providing),postcard <-> json, and arbitrary otherpostcard <-> Xtransformations can be performed by the same generic implementation.Unfortunately,
DeserializerandSerializermethods require&'static strs for structs/variants/field names. To provide these, the lossless implementation allocates and leaks one copy of each unique string that must be used as&'static. I can't find a way around this that isn't lossy and this seems inevitable in order to provide aSerializeablepostcard_dyn::Value. A separate, lossy transformation is also provided that does not leak memory but only supports json-like representations of structs/enums as maps.