simd_json/value/owned/
cmp.rs

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