Skip to content

Commit

Permalink
Merge pull request #2733 from jamessan/nan-decimal
Browse files Browse the repository at this point in the history
Only format Unexpected::Float with decimal point if it is finite
  • Loading branch information
dtolnay authored May 1, 2024
2 parents 1477028 + 6ca499b commit 2d973c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
18 changes: 11 additions & 7 deletions serde/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2312,13 +2312,17 @@ impl Display for WithDecimalPoint {
}
}

let mut writer = LookForDecimalPoint {
formatter,
has_decimal_point: false,
};
tri!(write!(writer, "{}", self.0));
if !writer.has_decimal_point {
tri!(formatter.write_str(".0"));
if self.0.is_finite() {
let mut writer = LookForDecimalPoint {
formatter,
has_decimal_point: false,
};
tri!(write!(writer, "{}", self.0));
if !writer.has_decimal_point {
tri!(formatter.write_str(".0"));
}
} else {
tri!(write!(formatter, "{}", self.0));
}
Ok(())
}
Expand Down
8 changes: 8 additions & 0 deletions test_suite/tests/test_de_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,14 @@ fn test_integer_from_float() {
);
}

#[test]
fn test_nan_no_decimal_point() {
assert_de_tokens_error::<isize>(
&[Token::F32(f32::NAN)],
"invalid type: floating point `NaN`, expected isize",
);
}

#[test]
fn test_unit_struct_from_seq() {
assert_de_tokens_error::<UnitStruct>(
Expand Down

0 comments on commit 2d973c1

Please sign in to comment.