simd_json/value/lazy/
cmp.rs

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