Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postcard-dyn: reserialize from arbitrary Deserializer into arbitrary Serializer #194

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

max-heller
Copy link
Collaborator

@max-heller max-heller commented Dec 1, 2024

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.

Copy link

netlify bot commented Dec 1, 2024

Deploy Preview for cute-starship-2d9c9b canceled.

Name Link
🔨 Latest commit 41bdb42
🔍 Latest deploy log https://app.netlify.com/sites/cute-starship-2d9c9b/deploys/67ad50f71009960008a9776a

@max-heller max-heller force-pushed the postcard-dyn-reserialize branch from 8d762a5 to 1fca580 Compare December 1, 2024 01:35
@jamesmunns
Copy link
Owner

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?

@max-heller
Copy link
Collaborator Author

max-heller commented Dec 2, 2024

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.

@max-heller
Copy link
Collaborator Author

max-heller commented Dec 2, 2024

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.

@max-heller max-heller force-pushed the postcard-dyn-reserialize branch 5 times, most recently from 16455e4 to e1400ba Compare December 3, 2024 23:53
@max-heller
Copy link
Collaborator Author

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

@max-heller
Copy link
Collaborator Author

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 max-heller marked this pull request as ready for review December 6, 2024 18:09
@max-heller max-heller marked this pull request as draft December 7, 2024 21:53
@max-heller max-heller force-pushed the postcard-dyn-reserialize branch 2 times, most recently from 374cc4f to dd37309 Compare December 16, 2024 13:35
@max-heller max-heller changed the title postcard-dyn: reserialize into arbitrary Serializers postcard-dyn: reserialize from arbitrary Deserializer into arbitrary Serializer Dec 16, 2024
@max-heller max-heller marked this pull request as ready for review December 16, 2024 13:40
@max-heller max-heller force-pushed the postcard-dyn-reserialize branch from dd37309 to 2f9fc3f Compare December 16, 2024 13:43
@max-heller max-heller added the postcard-schema Related to the postcard-schema crate label Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
postcard-schema Related to the postcard-schema crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants