Skip to content

Commit

Permalink
Touch up PR 814
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 5, 2021
1 parent a28b1b1 commit 0035947
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ mod lib {
pub use self::core::convert::{self, From, Into};
pub use self::core::default::{self, Default};
pub use self::core::fmt::{self, Debug, Display};
pub use self::core::hash::{self, Hash};
pub use self::core::hash::{self, Hash, Hasher};
pub use self::core::iter::FusedIterator;
pub use self::core::marker::{self, PhantomData};
pub use self::core::ops::{Bound, RangeBounds};
Expand Down
15 changes: 7 additions & 8 deletions src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,16 @@ impl PartialEq for N {
impl Eq for N {}

#[cfg(not(feature = "arbitrary_precision"))]
impl core::hash::Hash for N {
fn hash<H: core::hash::Hasher>(&self, h: &mut H) {
match self {
impl Hash for N {
fn hash<H: Hasher>(&self, h: &mut H) {
match *self {
N::PosInt(i) => i.hash(h),
N::NegInt(i) => i.hash(h),
N::Float(f) => {
// Using `f64::to_bits` here is fine since any float values are never `NaN`.
if *f == 0.0f64 {
// The IEEE 754 standard has two representations for zero, +0 and -0,
// such that +0 == -0.
// In both cases we use the +0 hash so that hash(+0) == hash(-0).
if f == 0.0f64 {
// There are 2 zero representations, +0 and -0, which
// compare equal but have different bits. We use the +0 hash
// for both so that hash(+0) == hash(-0).
0.0f64.to_bits().hash(h);
} else {
f.to_bits().hash(h);
Expand Down
13 changes: 9 additions & 4 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ use serde_json::{
from_reader, from_slice, from_str, from_value, json, to_string, to_string_pretty, to_value,
to_vec, to_writer, Deserializer, Number, Value,
};
use std::collections::hash_map::DefaultHasher;
use std::collections::BTreeMap;
use std::fmt::{self, Debug};
use std::hash::{Hash, Hasher};
use std::io;
use std::iter;
use std::marker::PhantomData;
Expand Down Expand Up @@ -2287,9 +2289,6 @@ fn test_value_into_deserializer() {

#[test]
fn hash_positive_and_negative_zero() {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

fn hash(obj: impl Hash) -> u64 {
let mut hasher = DefaultHasher::new();
obj.hash(&mut hasher);
Expand All @@ -2298,5 +2297,11 @@ fn hash_positive_and_negative_zero() {

let k1 = serde_json::from_str::<Number>("0.0").unwrap();
let k2 = serde_json::from_str::<Number>("-0.0").unwrap();
assert!(k1 != k2 || hash(k1) == hash(k2));
if cfg!(feature = "arbitrary_precision") {
assert_ne!(k1, k2);
assert_ne!(hash(k1), hash(k2));
} else {
assert_eq!(k1, k2);
assert_eq!(hash(k1), hash(k2));
}
}

0 comments on commit 0035947

Please sign in to comment.