Add discriminant to Dynamic::hash.

This commit is contained in:
Stephen Chung 2020-11-15 18:39:23 +08:00
parent c919ee4e46
commit fbe9425794

View File

@ -22,6 +22,7 @@ use crate::stdlib::{
boxed::Box, boxed::Box,
fmt, fmt,
hash::{Hash, Hasher}, hash::{Hash, Hasher},
mem,
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
string::{String, ToString}, string::{String, ToString},
}; };
@ -360,6 +361,8 @@ impl Dynamic {
impl Hash for Dynamic { impl Hash for Dynamic {
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
mem::discriminant(self).hash(state);
match &self.0 { match &self.0 {
Union::Unit(_) => ().hash(state), Union::Unit(_) => ().hash(state),
Union::Bool(value) => value.hash(state), Union::Bool(value) => value.hash(state),
@ -367,20 +370,17 @@ impl Hash for Dynamic {
Union::Char(ch) => ch.hash(state), Union::Char(ch) => ch.hash(state),
Union::Int(i) => i.hash(state), Union::Int(i) => i.hash(state),
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
Union::Float(f) => { Union::Float(f) => f.to_le_bytes().hash(state),
TypeId::of::<FLOAT>().hash(state);
state.write(&f.to_le_bytes());
}
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Union::Array(a) => a.hash(state), Union::Array(a) => (**a).hash(state),
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
Union::Map(m) => { Union::Map(m) => {
let mut buf: StaticVec<_> = m.keys().collect(); let mut buf: StaticVec<_> = m.iter().collect();
buf.sort(); buf.sort_by(|(a, _), (b, _)| a.cmp(b));
buf.into_iter().for_each(|key| { buf.into_iter().for_each(|(key, value)| {
key.hash(state); key.hash(state);
m[key].hash(state); value.hash(state);
}) })
} }