jsonbb/
partial_eq.rs

1// Copyright 2024 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! `PartialEq` implementations for `ValueRef` and `Value`.
16
17use crate::ValueRef;
18
19use super::Value;
20
21fn eq_i64(value: ValueRef<'_>, other: i64) -> bool {
22    value.as_i64() == Some(other)
23}
24
25fn eq_u64(value: ValueRef<'_>, other: u64) -> bool {
26    value.as_u64() == Some(other)
27}
28
29fn eq_f32(value: ValueRef<'_>, other: f32) -> bool {
30    match value {
31        ValueRef::Number(n) => n.as_f32() == Some(other),
32        _ => false,
33    }
34}
35
36fn eq_f64(value: ValueRef<'_>, other: f64) -> bool {
37    value.as_f64() == Some(other)
38}
39
40fn eq_bool(value: ValueRef<'_>, other: bool) -> bool {
41    value.as_bool() == Some(other)
42}
43
44fn eq_str(value: ValueRef<'_>, other: &str) -> bool {
45    value.as_str() == Some(other)
46}
47
48impl PartialEq<str> for ValueRef<'_> {
49    fn eq(&self, other: &str) -> bool {
50        eq_str(*self, other)
51    }
52}
53
54impl PartialEq<&str> for ValueRef<'_> {
55    fn eq(&self, other: &&str) -> bool {
56        eq_str(*self, other)
57    }
58}
59
60impl PartialEq<ValueRef<'_>> for str {
61    fn eq(&self, other: &ValueRef<'_>) -> bool {
62        eq_str(*other, self)
63    }
64}
65
66impl PartialEq<ValueRef<'_>> for &str {
67    fn eq(&self, other: &ValueRef<'_>) -> bool {
68        eq_str(*other, self)
69    }
70}
71
72impl PartialEq<String> for ValueRef<'_> {
73    fn eq(&self, other: &String) -> bool {
74        eq_str(*self, other.as_str())
75    }
76}
77
78impl PartialEq<ValueRef<'_>> for String {
79    fn eq(&self, other: &ValueRef<'_>) -> bool {
80        eq_str(*other, self.as_str())
81    }
82}
83
84macro_rules! partialeq_numeric {
85    ($($eq:ident [$($ty:ty)*])*) => {
86        $($(
87            impl PartialEq<$ty> for ValueRef<'_> {
88                fn eq(&self, other: &$ty) -> bool {
89                    $eq(*self, *other as _)
90                }
91            }
92
93            impl PartialEq<Value> for $ty {
94                fn eq(&self, other: &Value) -> bool {
95                    $eq(other.as_ref(), *self as _)
96                }
97            }
98
99            impl<'a> PartialEq<$ty> for &'a Value {
100                fn eq(&self, other: &$ty) -> bool {
101                    $eq(self.as_ref(), *other as _)
102                }
103            }
104        )*)*
105    }
106}
107
108partialeq_numeric! {
109    eq_i64[i8 i16 i32 i64 isize]
110    eq_u64[u8 u16 u32 u64 usize]
111    eq_f32[f32]
112    eq_f64[f64]
113    eq_bool[bool]
114}