simd_json/value/tape/
cmp.rs

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