Skip to main content

serde_json/value/
partial_eq.rs

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