-
Notifications
You must be signed in to change notification settings - Fork 123
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
Optionally always include float decimals #237
Conversation
Also, I'd love to use rust's fmt syntax to tack on the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I think this is a great feature, and I'd be happy to use it once available (presumably, in 0.6.1).
I have a few small concerns.
Also please consider adding a CHANGELOG entry for this.
src/ser/mod.rs
Outdated
@@ -363,11 +388,17 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> { | |||
|
|||
fn serialize_f32(self, v: f32) -> Result<()> { | |||
write!(self.output, "{}", v)?; | |||
if self.decimal_floats() && v - v.floor() == 0.0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why isn't this written as v == v.floor()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No reason. Your representation is clearly better 😄
tests/floats.rs
Outdated
#[test] | ||
fn decimal_floats() { | ||
let pretty = PrettyConfig::new().with_decimal_floats(false); | ||
let without_decimal = to_string_pretty(&1.0, pretty).expect("Serialization failed"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need those expect
strings here? For tests, I think unwrap
is perfectly fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was following the pattern in the "preserve_sequence" tests, but I'm happy to swap these out. "preserve_sequence" appears to be the only existing test that uses expect.
33c5539
to
d9a8305
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Note: clippy isn't happy with float comparisons (red on CI) |
i opted for the clippy recommended solution: |
oops :) |
It looks like EPSILON associated constants aren't in 1.34. Is backward compat desirable? I can either inline the epsilon values or someone will need to bump that version to 1.43. What should I do here? |
Let's not raise the Rust version just yet. Debian and other distros are typically much behind the top of the tree. |
This adds the option to always include the decimal point in floats.
Currently ron always serializes
5.0
as5
. I have a use case that makes the5
vs5.0
distinction important. My rust type is a dynamic collection that can contain either floats or ints. Round tripping a value of5.0
with ron results in an integer output of5
when its source was a float.