Skip to content

Commit a2038cf

Browse files
authored
Merge pull request #399 from serde-rs/map-or
Fewer instantiations of Option::map_or
2 parents b4e061f + a51990c commit a2038cf

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

src/value/partial_eq.rs

+35-15
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,96 @@
88

99
use super::Value;
1010

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+
1131
impl PartialEq<str> for Value {
1232
fn eq(&self, other: &str) -> bool {
13-
self.as_str().map_or(false, |s| s == other)
33+
eq_str(self, other)
1434
}
1535
}
1636

1737
impl<'a> PartialEq<&'a str> for Value {
1838
fn eq(&self, other: &&str) -> bool {
19-
self.as_str().map_or(false, |s| s == *other)
39+
eq_str(self, *other)
2040
}
2141
}
2242

2343
impl PartialEq<Value> for str {
2444
fn eq(&self, other: &Value) -> bool {
25-
other.as_str().map_or(false, |s| s == self)
45+
eq_str(other, self)
2646
}
2747
}
2848

2949
impl<'a> PartialEq<Value> for &'a str {
3050
fn eq(&self, other: &Value) -> bool {
31-
other.as_str().map_or(false, |s| s == *self)
51+
eq_str(other, *self)
3252
}
3353
}
3454

3555
impl PartialEq<String> for Value {
3656
fn eq(&self, other: &String) -> bool {
37-
self.as_str().map_or(false, |s| s == other)
57+
eq_str(self, other.as_str())
3858
}
3959
}
4060

4161

4262
impl PartialEq<Value> for String {
4363
fn eq(&self, other: &Value) -> bool {
44-
other.as_str().map_or(false, |s| s == self)
64+
eq_str(other, self.as_str())
4565
}
4666
}
4767

4868
macro_rules! partialeq_numeric {
49-
($([$($ty:ty)*], $conversion:ident, $base:ty)*) => {
69+
($($eq:ident [$($ty:ty)*])*) => {
5070
$($(
5171
impl PartialEq<$ty> for Value {
5272
fn eq(&self, other: &$ty) -> bool {
53-
self.$conversion().map_or(false, |i| i == (*other as $base))
73+
$eq(self, *other as _)
5474
}
5575
}
5676

5777
impl PartialEq<Value> for $ty {
5878
fn eq(&self, other: &Value) -> bool {
59-
other.$conversion().map_or(false, |i| i == (*self as $base))
79+
$eq(other, *self as _)
6080
}
6181
}
6282

6383
impl<'a> PartialEq<$ty> for &'a Value {
6484
fn eq(&self, other: &$ty) -> bool {
65-
self.$conversion().map_or(false, |i| i == (*other as $base))
85+
$eq(*self, *other as _)
6686
}
6787
}
6888

6989
impl<'a> PartialEq<$ty> for &'a mut Value {
7090
fn eq(&self, other: &$ty) -> bool {
71-
self.$conversion().map_or(false, |i| i == (*other as $base))
91+
$eq(*self, *other as _)
7292
}
7393
}
7494
)*)*
7595
}
7696
}
7797

7898
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]
83103
}

0 commit comments

Comments
 (0)