simd_json/value/owned/
cmp.rs

1use super::Value;
2use crate::{prelude::*, BorrowedValue};
3
4#[allow(clippy::cast_sign_loss, clippy::default_trait_access)]
5impl PartialEq<BorrowedValue<'_>> for Value {
6    #[cfg_attr(not(feature = "no-inline"), inline)]
7    #[must_use]
8    fn eq(&self, other: &BorrowedValue<'_>) -> bool {
9        match (self, other) {
10            (Self::Static(s1), BorrowedValue::Static(s2)) => s1 == s2,
11            (Self::String(v1), BorrowedValue::String(v2)) => v1.eq(v2),
12            (Self::Array(v1), BorrowedValue::Array(v2)) => v1.as_ref().eq(v2.as_ref()),
13            (Self::Object(v1), BorrowedValue::Object(v2)) => {
14                if v1.len() != v2.len() {
15                    return false;
16                }
17                v1.iter()
18                    .all(|(key, value)| v2.get(key.as_str()).map_or(false, |v| value == v))
19            }
20            _ => false,
21        }
22    }
23}
24
25#[allow(clippy::cast_sign_loss, clippy::default_trait_access)]
26impl PartialEq for Value {
27    #[cfg_attr(not(feature = "no-inline"), inline)]
28    #[must_use]
29    fn eq(&self, other: &Self) -> bool {
30        match (self, other) {
31            (Self::Static(s1), Self::Static(s2)) => s1.eq(s2),
32            (Self::String(v1), Self::String(v2)) => v1.eq(v2),
33            (Self::Array(v1), Self::Array(v2)) => v1.eq(v2),
34            (Self::Object(v1), Self::Object(v2)) => v1.eq(v2),
35            _ => false,
36        }
37    }
38}
39
40impl<T> PartialEq<&T> for Value
41where
42    Value: PartialEq<T>,
43{
44    #[cfg_attr(not(feature = "no-inline"), inline)]
45    #[must_use]
46    fn eq(&self, other: &&T) -> bool {
47        self == *other
48    }
49}
50
51impl PartialEq<()> for Value {
52    #[cfg_attr(not(feature = "no-inline"), inline)]
53    #[must_use]
54    fn eq(&self, _other: &()) -> bool {
55        self.is_null()
56    }
57}
58
59impl PartialEq<bool> for Value {
60    #[cfg_attr(not(feature = "no-inline"), inline)]
61    #[must_use]
62    fn eq(&self, other: &bool) -> bool {
63        self.as_bool().map(|t| t.eq(other)).unwrap_or_default()
64    }
65}
66
67impl PartialEq<str> for Value {
68    #[cfg_attr(not(feature = "no-inline"), inline)]
69    #[must_use]
70    fn eq(&self, other: &str) -> bool {
71        self.as_str().map(|t| t.eq(other)).unwrap_or_default()
72    }
73}
74
75impl PartialEq<&str> for Value {
76    #[cfg_attr(not(feature = "no-inline"), inline)]
77    #[must_use]
78    fn eq(&self, other: &&str) -> bool {
79        self == *other
80    }
81}
82
83impl PartialEq<String> for Value {
84    #[cfg_attr(not(feature = "no-inline"), inline)]
85    #[must_use]
86    fn eq(&self, other: &String) -> bool {
87        self.as_str().map(|t| t.eq(other)).unwrap_or_default()
88    }
89}
90
91impl PartialEq<i8> for Value {
92    #[cfg_attr(not(feature = "no-inline"), inline)]
93    #[must_use]
94    fn eq(&self, other: &i8) -> bool {
95        self.as_i8().map(|t| t.eq(other)).unwrap_or_default()
96    }
97}
98
99impl PartialEq<i16> for Value {
100    #[cfg_attr(not(feature = "no-inline"), inline)]
101    #[must_use]
102    fn eq(&self, other: &i16) -> bool {
103        self.as_i16().map(|t| t.eq(other)).unwrap_or_default()
104    }
105}
106
107impl PartialEq<i32> for Value {
108    #[cfg_attr(not(feature = "no-inline"), inline)]
109    #[must_use]
110    fn eq(&self, other: &i32) -> bool {
111        self.as_i32().map(|t| t.eq(other)).unwrap_or_default()
112    }
113}
114
115impl PartialEq<i64> for Value {
116    #[cfg_attr(not(feature = "no-inline"), inline)]
117    #[must_use]
118    fn eq(&self, other: &i64) -> bool {
119        self.as_i64().map(|t| t.eq(other)).unwrap_or_default()
120    }
121}
122
123impl PartialEq<i128> for Value {
124    #[cfg_attr(not(feature = "no-inline"), inline)]
125    #[must_use]
126    fn eq(&self, other: &i128) -> bool {
127        self.as_i128().map(|t| t.eq(other)).unwrap_or_default()
128    }
129}
130
131impl PartialEq<u8> for Value {
132    #[cfg_attr(not(feature = "no-inline"), inline)]
133    #[must_use]
134    fn eq(&self, other: &u8) -> bool {
135        self.as_u8().map(|t| t.eq(other)).unwrap_or_default()
136    }
137}
138
139impl PartialEq<u16> for Value {
140    #[cfg_attr(not(feature = "no-inline"), inline)]
141    #[must_use]
142    fn eq(&self, other: &u16) -> bool {
143        self.as_u16().map(|t| t.eq(other)).unwrap_or_default()
144    }
145}
146
147impl PartialEq<u32> for Value {
148    #[cfg_attr(not(feature = "no-inline"), inline)]
149    #[must_use]
150    fn eq(&self, other: &u32) -> bool {
151        self.as_u32().map(|t| t.eq(other)).unwrap_or_default()
152    }
153}
154
155impl PartialEq<u64> for Value {
156    #[cfg_attr(not(feature = "no-inline"), inline)]
157    #[must_use]
158    fn eq(&self, other: &u64) -> bool {
159        self.as_u64().map(|t| t.eq(other)).unwrap_or_default()
160    }
161}
162
163impl PartialEq<usize> for Value {
164    #[cfg_attr(not(feature = "no-inline"), inline)]
165    #[must_use]
166    fn eq(&self, other: &usize) -> bool {
167        self.as_usize().map(|t| t.eq(other)).unwrap_or_default()
168    }
169}
170
171impl PartialEq<u128> for Value {
172    #[cfg_attr(not(feature = "no-inline"), inline)]
173    #[must_use]
174    fn eq(&self, other: &u128) -> bool {
175        self.as_u128().map(|t| t.eq(other)).unwrap_or_default()
176    }
177}
178
179impl PartialEq<f32> for Value {
180    #[cfg_attr(not(feature = "no-inline"), inline)]
181    #[must_use]
182    fn eq(&self, other: &f32) -> bool {
183        self.as_f32().map(|t| t.eq(other)).unwrap_or_default()
184    }
185}
186
187impl PartialEq<f64> for Value {
188    #[cfg_attr(not(feature = "no-inline"), inline)]
189    #[must_use]
190    fn eq(&self, other: &f64) -> bool {
191        self.as_f64().map(|t| t.eq(other)).unwrap_or_default()
192    }
193}
194
195impl<T> PartialEq<&[T]> for Value
196where
197    Value: PartialEq<T>,
198{
199    #[cfg_attr(not(feature = "no-inline"), inline)]
200    #[must_use]
201    fn eq(&self, other: &&[T]) -> bool {
202        self.as_array().map(|t| t.eq(other)).unwrap_or_default()
203    }
204}
205impl<K, T, S> PartialEq<std::collections::HashMap<K, T, S>> for Value
206where
207    K: AsRef<str> + std::hash::Hash + Eq,
208    Value: PartialEq<T>,
209    S: std::hash::BuildHasher,
210{
211    #[cfg_attr(not(feature = "no-inline"), inline)]
212    #[must_use]
213    fn eq(&self, other: &std::collections::HashMap<K, T, S>) -> bool {
214        self.as_object().map_or(false, |object| {
215            object.len() == other.len()
216                && other
217                    .iter()
218                    .all(|(key, value)| object.get(key.as_ref()).map_or(false, |v| *v == *value))
219        })
220    }
221}