serde_json/value/
partial_eq.rs

1use super::Value;
2
3fn eq_i64(value: &Value, other: i64) -> bool {
4    value.as_i64().map_or(false, |i| i == other)
5}
6
7fn eq_u64(value: &Value, other: u64) -> bool {
8    value.as_u64().map_or(false, |i| i == other)
9}
10
11fn eq_f64(value: &Value, other: f64) -> bool {
12    value.as_f64().map_or(false, |i| i == other)
13}
14
15fn eq_bool(value: &Value, other: bool) -> bool {
16    value.as_bool().map_or(false, |i| i == other)
17}
18
19fn eq_str(value: &Value, other: &str) -> bool {
20    value.as_str().map_or(false, |i| i == other)
21}
22
23impl PartialEq<str> for Value {
24    fn eq(&self, other: &str) -> bool {
25        eq_str(self, other)
26    }
27}
28
29impl<'a> PartialEq<&'a str> for Value {
30    fn eq(&self, other: &&str) -> bool {
31        eq_str(self, *other)
32    }
33}
34
35impl PartialEq<Value> for str {
36    fn eq(&self, other: &Value) -> bool {
37        eq_str(other, self)
38    }
39}
40
41impl<'a> PartialEq<Value> for &'a str {
42    fn eq(&self, other: &Value) -> bool {
43        eq_str(other, *self)
44    }
45}
46
47impl PartialEq<String> for Value {
48    fn eq(&self, other: &String) -> bool {
49        eq_str(self, other.as_str())
50    }
51}
52
53impl PartialEq<Value> for String {
54    fn eq(&self, other: &Value) -> bool {
55        eq_str(other, self.as_str())
56    }
57}
58
59macro_rules! partialeq_numeric {
60    ($($eq:ident [$($ty:ty)*])*) => {
61        $($(
62            impl PartialEq<$ty> for Value {
63                fn eq(&self, other: &$ty) -> bool {
64                    $eq(self, *other as _)
65                }
66            }
67
68            impl PartialEq<Value> for $ty {
69                fn eq(&self, other: &Value) -> bool {
70                    $eq(other, *self as _)
71                }
72            }
73
74            impl<'a> PartialEq<$ty> for &'a Value {
75                fn eq(&self, other: &$ty) -> bool {
76                    $eq(*self, *other as _)
77                }
78            }
79
80            impl<'a> PartialEq<$ty> for &'a mut Value {
81                fn eq(&self, other: &$ty) -> bool {
82                    $eq(*self, *other as _)
83                }
84            }
85        )*)*
86    }
87}
88
89partialeq_numeric! {
90    eq_i64[i8 i16 i32 i64 isize]
91    eq_u64[u8 u16 u32 u64 usize]
92    eq_f64[f32 f64]
93    eq_bool[bool]
94}