Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Deterministically destroy sign of NaN when converted to Number #394

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,22 +517,24 @@ macro_rules! from_unsigned {
};
}

macro_rules! from_float {
($($float_ty:ident)*) => {
$(
impl From<$float_ty> for Number {
#[inline]
fn from(f: $float_ty) -> Self {
Number { n: N::Float(f as f64) }
}
}
)*
from_signed!(i8 i16 i32 i64 isize);
from_unsigned!(u8 u16 u32 u64 usize);

impl From<f32> for Number {
fn from(f: f32) -> Self {
Number::from(f as f64)
}
}

from_signed!(i8 i16 i32 i64 isize);
from_unsigned!(u8 u16 u32 u64 usize);
from_float!(f32 f64);
impl From<f64> for Number {
fn from(mut f: f64) -> Self {
if f.is_nan() {
// Destroy NaN sign, signaling, and payload. YAML only has one NaN.
f = f64::NAN.copysign(1.0);
}
Number { n: N::Float(f) }
}
}

// This is fine, because we don't _really_ implement hash for floats
// all other hash functions should work as expected
Expand Down