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

Serialize and deserialize ConstrainedFloats as floats #25

Closed

Conversation

benjaminrsherman
Copy link

Currently Serde serializes ConstrainedFloats with a PhantomData field and fails to deserialize raw floats. This change causes Serde to treat ConstrainedFloats as if they were a normal float which is being constructed.

I'm not sure if the #[serde(transparent)] is needed with #[repr(transparent)], but I included it just in case.

Currently Serde serializes ConstrainedFloats with a PhantomData field and fails
to deserialize raw floats.  This change causes Serde to treat
ConstrainedFloats as if they were a normal float which is being
constructed.
@olson-sean-k
Copy link
Owner

Thanks for the PR!

An identical change is slated to land in the 0.4.0 release. It's currently in 2ddc201 on the v0.4.0 branch (but that commit will likely jump around as changes are rebased). That change and similar serialization issues have been discussed in #5.

I've been maintaining upcoming breaking changes on the v0.4.0 branch instead of master, but there's nothing in the repository that points this out. Sorry about that. I'm still not sure exactly how I'd like to manage breaking changes and backporting. Once I know, I'll be sure to document it somewhere.

@olson-sean-k
Copy link
Owner

After thinking about it some more, I don't think the v0.4.0 branch should be a thing at all. 😝 It probably should've been a feature branch and not some kind of version branch. Let's go ahead and merge this and I'll sort out that branch later.

Thanks again for putting this together.

@bjchambers
Copy link

Just dropping in to say this change (or something like it) would be appreciated!

@olson-sean-k
Copy link
Owner

olson-sean-k commented Sep 8, 2021

The previously mentioned change landed on master at 89063f3. However, that change (which ignores the phantom field) still results in serialization as a structure with a single field. Using transparent de/serialization via serde(transparent) isn't sufficient, as it bypasses any constraints applied by proxy types. That can be especially problematic, since the serialized format gives no indication that the data is anything else than a floating-point primitive.

I'm closing this for now, but have opened #36 to track work for transparent de/serialization that enforces constraints. There's also a known issue regarding serde_json (and presumably other formats) wherein non-real values of floating-point primitives cannot be serialized.

Thanks again for the PR and discussion! Sorry I dragged my feet on this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants