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