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

Allow floats to decode from integer terms #641

Merged
merged 1 commit into from
Jul 25, 2024

Conversation

benhaney
Copy link
Contributor

Because of the weak difference in Elixir/Erlang between integers and floats (collectively "numbers"), it's common for a number that the BEAM stores internally as an integer to end up where a float was expected, usually with no consequences until we try to leave the erlang world and enter the very strongly typed Rust world.

This PR changes the decoder for f64 and f32 to transparently work on erlang terms that are internally integers. If decoding a term to f64 fails, it simply tries again as i64 and then casts it to the appropriate float type.

There's a lot of breaking up and reorganizing of macros in here too, both to cut down on boilerplate and to allow encoder/decoder impls to be generated separately (we only need to manually define float decoders, but the generic macros can still generate the encoders), but I can definitely cut that down if this is a "too much macro" situation.

Fixes #603

@filmor filmor merged commit 49d4773 into rusterlium:master Jul 25, 2024
46 checks passed
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.

Transparently decode mixed lists of floats/integers into f64
2 participants