1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use super::*;

use crate::builtins::Number;
use std::hash::{Hash, Hasher};

impl PartialEq for Value {
    fn eq(&self, other: &Self) -> bool {
        same_value_zero(self, other)
    }
}

impl Eq for Value {}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct UndefinedHashable;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct NullHashable;

#[derive(Debug, Clone, Copy)]
struct RationalHashable(f64);

impl PartialEq for RationalHashable {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        Number::same_value(self.0, other.0)
    }
}

impl Eq for RationalHashable {}

impl Hash for RationalHashable {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.to_bits().hash(state);
    }
}

impl Hash for Value {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            Self::Undefined => UndefinedHashable.hash(state),
            Self::Null => NullHashable.hash(state),
            Self::String(ref string) => string.hash(state),
            Self::Boolean(boolean) => boolean.hash(state),
            Self::Integer(integer) => integer.hash(state),
            Self::BigInt(ref bigint) => bigint.hash(state),
            Self::Rational(rational) => RationalHashable(*rational).hash(state),
            Self::Symbol(ref symbol) => Hash::hash(symbol, state),
            Self::Object(ref object) => std::ptr::hash(object.as_ref(), state),
        }
    }
}