platform_value/
eq.rs

1use crate::Value;
2
3macro_rules! implpartialeq {
4    ($($t:ty),+ $(,)?) => {
5        $(
6            impl PartialEq<$t> for Value {
7                #[inline]
8                fn eq(&self, other: &$t) -> bool {
9                    if let Some(i) = self.as_integer::<$t>() {
10                        &i == other
11                    } else {
12                        false
13                    }
14                }
15            }
16
17            impl PartialEq<$t> for &Value {
18                #[inline]
19                fn eq(&self, other: &$t) -> bool {
20                    if let Some(i) = self.as_integer::<$t>() {
21                        &i == other
22                    } else {
23                        false
24                    }
25                }
26            }
27        )+
28    };
29}
30
31implpartialeq! {
32    u128,
33    u64,
34    u32,
35    u16,
36    u8,
37    i128,
38    i64,
39    i32,
40    i16,
41    i8,
42}
43
44impl PartialEq<String> for Value {
45    #[inline]
46    fn eq(&self, other: &String) -> bool {
47        if let Some(i) = self.as_text() {
48            i == other
49        } else {
50            false
51        }
52    }
53}
54
55impl PartialEq<String> for &Value {
56    #[inline]
57    fn eq(&self, other: &String) -> bool {
58        if let Some(i) = self.as_str() {
59            i == other
60        } else {
61            false
62        }
63    }
64}
65
66impl PartialEq<&str> for Value {
67    #[inline]
68    fn eq(&self, other: &&str) -> bool {
69        if let Some(i) = self.as_str() {
70            &i == other
71        } else {
72            false
73        }
74    }
75}
76
77impl PartialEq<&str> for &Value {
78    #[inline]
79    fn eq(&self, other: &&str) -> bool {
80        if let Some(i) = self.as_str() {
81            &i == other
82        } else {
83            false
84        }
85    }
86}
87
88impl PartialEq<f64> for Value {
89    #[inline]
90    fn eq(&self, other: &f64) -> bool {
91        if let Some(i) = self.as_float() {
92            &i == other
93        } else {
94            false
95        }
96    }
97}
98
99impl PartialEq<f64> for &Value {
100    #[inline]
101    fn eq(&self, other: &f64) -> bool {
102        if let Some(i) = self.as_float() {
103            &i == other
104        } else {
105            false
106        }
107    }
108}
109
110impl PartialEq<Vec<u8>> for Value {
111    #[inline]
112    fn eq(&self, other: &Vec<u8>) -> bool {
113        self.as_bytes_slice() == Ok(other.as_slice())
114    }
115}
116impl PartialEq<Vec<u8>> for &Value {
117    #[inline]
118    fn eq(&self, other: &Vec<u8>) -> bool {
119        self.as_bytes_slice() == Ok(other.as_slice())
120    }
121}
122
123macro_rules! impl_bytes_array_eq {
124    ($($n:expr),+ $(,)?) => {$(
125        impl PartialEq<[u8; $n]> for Value {
126            #[inline]
127            fn eq(&self, other: &[u8; $n]) -> bool {
128                self.as_bytes_slice() == Ok(other.as_slice())
129            }
130        }
131        impl PartialEq<[u8; $n]> for &Value {
132            #[inline]
133            fn eq(&self, other: &[u8; $n]) -> bool {
134                self.as_bytes_slice() == Ok(other.as_slice())
135            }
136        }
137    )+};
138}
139impl_bytes_array_eq! { 20, 32, 36 }
140
141impl Value {
142    /* -------------------------------------------------------- *
143     *  equality on underlying data                             *
144     * -------------------------------------------------------- */
145
146    /// Returns `true` when the *data* represented by the two `Value`s
147    /// is identical, even if they are stored in different but
148    /// compatible variants.
149    ///
150    /// * All “bytes-like” variants (`Bytes`, `Bytes20`, `Bytes32`,
151    ///   `Bytes36`, `Identifier`) compare equal when their byte
152    ///   sequences match.
153    /// * All integer variants (`U*`, `I*`) compare equal when they
154    ///   represent the same numeric value.
155    /// * Otherwise falls back to normal `==` (`PartialEq`) behaviour.
156    #[inline]
157    pub fn equal_underlying_data(&self, other: &Value) -> bool {
158        // 1) bytes-like cross-variant equality
159        if let (Ok(a), Ok(b)) = (self.as_bytes_slice(), other.as_bytes_slice()) {
160            return a == b;
161        }
162
163        // 2) integer cross-variant equality
164        if let (Some(a), Some(b)) = (self.as_i128_unified(), other.as_i128_unified()) {
165            return a == b;
166        }
167
168        // 3) default
169        self == other
170    }
171}