Skip to content

Commit 7092d0e

Browse files
authored
Remove decimal_floats config but always include decimals for Value::Number(Float) roundtrip (#363)
* Enable `decimal_floats` by default for `Value::Number(Float)` roundtrip * Added Changelog entry * Removed `decimal_floats` `PrettyConfig` option
1 parent 8942818 commit 7092d0e

File tree

7 files changed

+43
-43
lines changed

7 files changed

+43
-43
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- Fix issue [#301](https://github.com/ron-rs/ron/issues/301) with better error messages ([#354](https://github.com/ron-rs/ron/pull/354))
2020
- Fix issue [#152](https://github.com/ron-rs/ron/issues/152) bitflags serde ([#352](https://github.com/ron-rs/ron/pull/352))
2121
- Fix issue [#359](https://github.com/ron-rs/ron/issues/359) with `DeserializeSeed` support ([#360](https://github.com/ron-rs/ron/pull/360))
22-
- Bump MSRV to 1.46.0
22+
- Bump MSRV to 1.46.0 ([#361](https://github.com/ron-rs/ron/pull/361))
23+
- Fix issue [#337](https://github.com/ron-rs/ron/issues/337) by removing `decimal_floats` PrettyConfig option and unconditional decimals in floats ([#363](https://github.com/ron-rs/ron/pull/363))
2324

2425
## [0.7.0] - 2021-10-22
2526

src/ser/mod.rs

+2-22
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ pub struct PrettyConfig {
8585
pub separate_tuple_members: bool,
8686
/// Enumerate array items in comments
8787
pub enumerate_arrays: bool,
88-
/// Always include the decimal in floats
89-
pub decimal_floats: bool,
9088
/// Enable extensions. Only configures 'implicit_some',
9189
/// 'unwrap_newtypes', and 'unwrap_variant_newtypes' for now.
9290
pub extensions: Extensions,
@@ -170,17 +168,6 @@ impl PrettyConfig {
170168
self
171169
}
172170

173-
/// Configures whether floats should always include a decimal.
174-
/// When false `1.0` will serialize as `1`
175-
/// When true `1.0` will serialize as `1.0`
176-
///
177-
/// Default: `false`
178-
pub fn decimal_floats(mut self, decimal_floats: bool) -> Self {
179-
self.decimal_floats = decimal_floats;
180-
181-
self
182-
}
183-
184171
/// Configures whether every array should be a single line (true) or a multi line one (false)
185172
/// When false, `["a","b"]` (as well as any array) will serialize to
186173
/// `
@@ -223,7 +210,6 @@ impl Default for PrettyConfig {
223210
separate_tuple_members: false,
224211
enumerate_arrays: false,
225212
extensions: Extensions::empty(),
226-
decimal_floats: false,
227213
compact_arrays: false,
228214
}
229215
}
@@ -298,12 +284,6 @@ impl<W: io::Write> Serializer<W> {
298284
.map_or(false, |&(ref config, _)| config.separate_tuple_members)
299285
}
300286

301-
fn decimal_floats(&self) -> bool {
302-
self.pretty
303-
.as_ref()
304-
.map_or(false, |&(ref config, _)| config.decimal_floats)
305-
}
306-
307287
fn compact_arrays(&self) -> bool {
308288
self.pretty
309289
.as_ref()
@@ -463,15 +443,15 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
463443

464444
fn serialize_f32(self, v: f32) -> Result<()> {
465445
write!(self.output, "{}", v)?;
466-
if self.decimal_floats() && (v - v.floor()).abs() < f32::EPSILON {
446+
if (v - v.floor()).abs() < f32::EPSILON {
467447
write!(self.output, ".0")?;
468448
}
469449
Ok(())
470450
}
471451

472452
fn serialize_f64(self, v: f64) -> Result<()> {
473453
write!(self.output, "{}", v)?;
474-
if self.decimal_floats() && (v - v.floor()).abs() < f64::EPSILON {
454+
if (v - v.floor()).abs() < f64::EPSILON {
475455
write!(self.output, ".0")?;
476456
}
477457
Ok(())

src/ser/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn test_empty_struct() {
3131
fn test_struct() {
3232
let my_struct = MyStruct { x: 4.0, y: 7.0 };
3333

34-
assert_eq!(to_string(&my_struct).unwrap(), "(x:4,y:7)");
34+
assert_eq!(to_string(&my_struct).unwrap(), "(x:4.0,y:7.0)");
3535

3636
#[derive(Serialize)]
3737
struct NewType(i32);
@@ -41,7 +41,7 @@ fn test_struct() {
4141
#[derive(Serialize)]
4242
struct TupleStruct(f32, f32);
4343

44-
assert_eq!(to_string(&TupleStruct(2.0, 5.0)).unwrap(), "(2,5)");
44+
assert_eq!(to_string(&TupleStruct(2.0, 5.0)).unwrap(), "(2.0,5.0)");
4545
}
4646

4747
#[test]

src/value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl Eq for Float {}
280280

281281
impl Hash for Float {
282282
fn hash<H: Hasher>(&self, state: &mut H) {
283-
state.write_u64(self.0 as u64);
283+
state.write_u64(self.0.to_bits());
284284
}
285285
}
286286

tests/337_value_float_roundtrip.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#[test]
2+
fn roundtrip_value_float_with_decimals() {
3+
let v: ron::Value = ron::from_str("1.0").unwrap();
4+
5+
assert_eq!(v, ron::Value::Number(1.0_f64.into()));
6+
7+
let ser = ron::ser::to_string(&v).unwrap();
8+
9+
let roundtrip = ron::from_str(&ser).unwrap();
10+
11+
assert_eq!(v, roundtrip);
12+
}
13+
14+
#[test]
15+
#[allow(clippy::float_cmp)]
16+
fn roundtrip_value_float_into() {
17+
let v: ron::Value = ron::from_str("1.0").unwrap();
18+
assert_eq!(v, ron::Value::Number(1.0_f64.into()));
19+
20+
let ser = ron::ser::to_string(&v).unwrap();
21+
22+
let f1: f64 = ron::from_str(&ser).unwrap();
23+
assert_eq!(f1, 1.0_f64);
24+
25+
let roundtrip: ron::Value = ron::from_str(&ser).unwrap();
26+
27+
let f2: f64 = roundtrip.into_rust().unwrap();
28+
assert_eq!(f2, 1.0_f64);
29+
}

tests/floats.rs

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ron::{
22
de::from_str,
3-
ser::{to_string_pretty, PrettyConfig},
3+
ser::{to_string, to_string_pretty, PrettyConfig},
44
};
55

66
#[test]
@@ -12,19 +12,9 @@ fn test_inf_and_nan() {
1212

1313
#[test]
1414
fn decimal_floats() {
15-
let pretty = PrettyConfig::new().decimal_floats(false);
16-
let without_decimal = to_string_pretty(&1.0, pretty).unwrap();
17-
assert_eq!(without_decimal, "1");
15+
let non_pretty = to_string(&1.0).unwrap();
16+
assert_eq!(non_pretty, "1.0");
1817

19-
let pretty = PrettyConfig::new().decimal_floats(false);
20-
let without_decimal = to_string_pretty(&1.1, pretty).unwrap();
21-
assert_eq!(without_decimal, "1.1");
22-
23-
let pretty = PrettyConfig::new().decimal_floats(true);
24-
let with_decimal = to_string_pretty(&1.0, pretty).unwrap();
25-
assert_eq!(with_decimal, "1.0");
26-
27-
let pretty = PrettyConfig::new().decimal_floats(true);
28-
let with_decimal = to_string_pretty(&1.1, pretty).unwrap();
29-
assert_eq!(with_decimal, "1.1");
18+
let with_pretty = to_string_pretty(&1.0, PrettyConfig::new()).unwrap();
19+
assert_eq!(with_pretty, "1.0");
3020
}

tests/to_string_pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ fn test_struct_names() {
1414
let struct_name = to_string_pretty(&value, PrettyConfig::default().struct_names(true));
1515
assert_eq!(
1616
struct_name,
17-
Ok("Point(\n x: 1,\n y: 2,\n)".to_string())
17+
Ok("Point(\n x: 1.0,\n y: 2.0,\n)".to_string())
1818
);
1919
let no_struct_name = to_string(&value);
20-
assert_eq!(no_struct_name, Ok("(x:1,y:2)".to_string()));
20+
assert_eq!(no_struct_name, Ok("(x:1.0,y:2.0)".to_string()));
2121
}

0 commit comments

Comments
 (0)