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