Allow floats to decode from integer terms #641
Merged
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.
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