simd_json/value/borrowed/
cmp.rs

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