Skip to main content

rbatis_codegen/
ops_eq.rs

1use crate::ops::PartialEq;
2use rbs::Value;
3use std::borrow::Cow;
4use std::cmp::PartialEq as PE;
5use std::ops::Deref;
6
7#[inline]
8fn eq_i64(value: i64, rhs: i64) -> bool {
9    value == rhs
10}
11
12#[inline]
13fn eq_f64(value: f64, rhs: f64) -> bool {
14    value == rhs
15}
16
17#[inline]
18fn eq_bool(value: bool, rhs: bool) -> bool {
19    value == rhs
20}
21
22#[inline]
23fn eq_str(value: &str, rhs: &str) -> bool {
24    value == rhs
25}
26
27#[inline]
28fn into_i64(value: &Value) -> i64 {
29    value.as_i64().unwrap_or_default()
30}
31
32#[inline]
33fn into_f64(value: &Value) -> f64 {
34    value.as_f64().unwrap_or_default()
35}
36
37#[inline]
38fn into_bool(value: &Value) -> bool {
39    value.as_bool().unwrap_or_default()
40}
41
42impl PartialEq<Value> for &Value {
43    fn op_eq(&self, other: &Value) -> bool {
44        self.eq(&other)
45    }
46}
47
48impl PartialEq<&Value> for &Value {
49    fn op_eq(&self, other: &&Value) -> bool {
50        self.eq(other)
51    }
52}
53
54impl PartialEq<&&Value> for &Value {
55    fn op_eq(&self, other: &&&Value) -> bool {
56        self.eq(&**other)
57    }
58}
59
60impl PartialEq<&&Value> for &&Value {
61    fn op_eq(&self, other: &&&Value) -> bool {
62        self.eq(other)
63    }
64}
65
66impl PartialEq<Value> for &&Value {
67    fn op_eq(&self, other: &Value) -> bool {
68        (*self).eq(&other)
69    }
70}
71
72impl PartialEq<&Value> for Value {
73    fn op_eq(&self, other: &&Value) -> bool {
74        self.eq(&**other)
75    }
76}
77
78impl PartialEq<&&Value> for Value {
79    fn op_eq(&self, other: &&&Value) -> bool {
80        self.eq(&***other)
81    }
82}
83
84impl PartialEq<Value> for Value {
85    fn op_eq(&self, other: &Value) -> bool {
86        self.eq(other)
87    }
88}
89
90impl PartialEq<str> for Value {
91    fn op_eq(&self, other: &str) -> bool {
92        eq_str(self.as_str().unwrap_or_default(), other)
93    }
94}
95
96impl PartialEq<&str> for Value {
97    fn op_eq(&self, other: &&str) -> bool {
98        eq_str(self.as_str().unwrap_or_default(), other)
99    }
100}
101
102impl PartialEq<Value> for str {
103    fn op_eq(&self, other: &Value) -> bool {
104        eq_str(other.as_str().unwrap_or_default(), self)
105    }
106}
107
108impl PartialEq<Value> for &str {
109    fn op_eq(&self, other: &Value) -> bool {
110        eq_str(other.as_str().unwrap_or_default(), self)
111    }
112}
113
114impl PartialEq<&str> for str {
115    fn op_eq(&self, other: &&str) -> bool {
116        self.eq(*other)
117    }
118}
119
120impl PartialEq<String> for Value {
121    fn op_eq(&self, other: &String) -> bool {
122        eq_str(self.as_str().unwrap_or_default(), other.as_str())
123    }
124}
125
126impl PartialEq<String> for &Value {
127    fn op_eq(&self, other: &String) -> bool {
128        eq_str(self.as_str().unwrap_or_default(), other.as_str())
129    }
130}
131
132impl PartialEq<&str> for &Value {
133    fn op_eq(&self, other: &&str) -> bool {
134        eq_str(self.as_str().unwrap_or_default(), other)
135    }
136}
137
138impl PartialEq<Value> for String {
139    fn op_eq(&self, other: &Value) -> bool {
140        eq_str(other.as_str().unwrap_or_default(), self.as_str())
141    }
142}
143
144impl PartialEq<Cow<'_, Value>> for Value {
145    fn op_eq(&self, other: &Cow<'_, Value>) -> bool {
146        self.eq(other.deref())
147    }
148}
149
150macro_rules! impl_numeric_eq {
151    ($($eq:ident,$into:ident [$($ty:ty)*])*) => {
152        $($(
153            impl PartialEq<$ty> for Value {
154               fn op_eq(&self, other: &$ty) -> bool {
155                    $eq($into(self), *other as _)
156                }
157            }
158
159            impl PartialEq<&$ty> for Value {
160               fn op_eq(&self, other: &&$ty) -> bool {
161                    $eq($into(self), **other as _)
162                }
163            }
164
165            impl<'a> PartialEq<$ty> for &'a Value {
166               fn op_eq(&self, other: &$ty) -> bool {
167                    $eq($into(*self), *other as _)
168                }
169            }
170
171            impl<'a> PartialEq<&$ty> for &'a Value {
172               fn op_eq(&self, other: &&$ty) -> bool {
173                    $eq($into(*self), **other as _)
174                }
175            }
176
177            impl PartialEq<Value> for $ty {
178               fn op_eq(&self, other: &Value) -> bool {
179                    $eq($into(other), *self as _)
180                }
181            }
182
183            impl PartialEq<&Value> for $ty {
184               fn op_eq(&self, other: &&Value)  -> bool {
185                    $eq($into(*other), *self as _)
186                }
187            }
188
189            impl PartialEq<Value> for &$ty {
190               fn op_eq(&self, other: &Value) -> bool {
191                    $eq($into(other), **self as _)
192                }
193            }
194
195            impl PartialEq<&Value> for &$ty {
196               fn op_eq(&self, other: &&Value)  -> bool {
197                    $eq($into(*other), **self as _)
198                }
199            }
200            // for unary
201            impl PartialEq<&&Value> for $ty {
202               fn op_eq(&self, other: &&&Value)  -> bool {
203                    $eq($into(**other), *self as _)
204                }
205            }
206        )*)*
207    }
208}
209
210impl_numeric_eq! {
211    eq_i64,into_i64[u8 u16 u32 u64]
212    eq_i64,into_i64[i8 i16 i32 i64 isize usize]
213    eq_f64,into_f64[f32 f64]
214    eq_bool,into_bool[bool]
215}
216
217macro_rules! self_eq {
218    ([$($ty:ty)*]) => {
219        $(
220impl PartialEq<$ty> for $ty{
221      fn op_eq(&self, rhs: &$ty) -> bool {
222          self.eq(rhs)
223      }
224    }
225impl PartialEq<&$ty> for $ty{
226      fn op_eq(&self, rhs: &&$ty) -> bool {
227         self.eq(*rhs)
228      }
229    }
230impl PartialEq<$ty> for &$ty{
231      fn op_eq(&self, rhs: &$ty) -> bool {
232          self.eq(&rhs)
233      }
234    }
235impl PartialEq<&$ty> for &$ty{
236      fn op_eq(&self, rhs: &&$ty) -> bool {
237          self.eq(rhs)
238      }
239    }
240        )*
241    };
242}
243
244self_eq!([u8 u16 u32 u64]);
245self_eq!([i8 i16 i32 i64 isize usize]);
246self_eq!([f32 f64]);
247self_eq!([String & str]);
248
249macro_rules! impl_str_eq {
250    ($($eq:ident [$($ty:ty)*])*) => {
251        $($(
252            impl PartialEq<$ty> for &str {
253               fn op_eq(&self, other: &$ty) -> bool {
254                    $eq(self, *other as _)
255                }
256            }
257
258            impl PartialEq<&str> for $ty {
259               fn op_eq(&self, other: &&str) -> bool {
260                    $eq(other, *self as _)
261                }
262            }
263
264            impl PartialEq<&&str> for $ty {
265               fn op_eq(&self, other: &&&str)  -> bool {
266                    $eq(*other, *self as _)
267                }
268            }
269
270            impl PartialEq<&&&str> for $ty {
271               fn op_eq(&self, other: &&&&str)  -> bool {
272                    $eq(**other, *self as _)
273                }
274            }
275
276            impl<'a> PartialEq<$ty> for &'a &str {
277               fn op_eq(&self, other: &$ty) -> bool {
278                    $eq(*self, *other as _)
279                }
280            }
281        )*)*
282    }
283}
284
285fn eq_str_i64(value: &str, other: i64) -> bool {
286    value.eq(&other.to_string())
287}
288
289fn eq_str_f64(value: &str, other: f64) -> bool {
290    value.eq(&other.to_string())
291}
292
293fn eq_str_bool(value: &str, other: bool) -> bool {
294    match value {
295        "true" => {
296            other
297        }
298        "false" => {
299            !other
300        }
301        _ => false,
302    }
303}
304impl_str_eq! {
305    eq_str_i64[u8 u16 u32 u64]
306    eq_str_i64[i8 i16 i32 i64 isize usize]
307    eq_str_f64[f32 f64]
308    eq_str_bool[bool]
309}
310
311macro_rules! eq_diff {
312   ($eq:ident[$(($ty1:ty,$ty2:ty),)*]) => {
313        $(
314        impl PartialEq<$ty1> for $ty2{
315          fn op_eq(&self, rhs: &$ty1) -> bool {
316              rhs.eq(&(*self as $ty1))
317          }
318        }
319        impl PartialEq<&$ty1> for $ty2{
320          fn op_eq(&self, rhs: &&$ty1) -> bool {
321             (*rhs).eq(&(*self as $ty1))
322          }
323        }
324        impl PartialEq<$ty1> for &$ty2{
325          fn op_eq(&self, rhs: &$ty1) -> bool {
326             rhs.eq(&(**self as $ty1))
327          }
328        }
329        impl PartialEq<&$ty1> for &$ty2{
330          fn op_eq(&self, rhs: &&$ty1) -> bool {
331             (*rhs).eq(&(**self as $ty1))
332          }
333        }
334        )*
335    };
336}
337
338eq_diff!(eq_i64[(i64,i8),(i64,i16),(i64,i32),]);
339eq_diff!(eq_i64[(i64,u8),(i64,u16),(i64,u32),(i64,u64),(i64,usize),]);
340eq_diff!(eq_f64[(i64,f32),(i64,f64),]);
341
342eq_diff!(eq_i64[(u64,i8),(u64,i16),(u64,i32),(u64,i64),]);
343eq_diff!(eq_u64[(u64,u8),(u64,u16),(u64,u32),(u64,usize),]);
344eq_diff!(eq_f64[(u64,f32),(u64,f64),]);
345
346eq_diff!(eq_f64[(f64,u8),(f64,u16),(f64,u32),(f64,u64),(f64,usize),]);
347eq_diff!(eq_f64[(f64,i8),(f64,i16),(f64,i32),(f64,i64),]);
348eq_diff!(eq_f64[(f64,f32),]);
349
350#[cfg(test)]
351mod test {
352    use crate::ops::Add;
353    use rbs::{value, Value};
354
355    #[test]
356    fn test_eq() {
357        let i: i64 = 1;
358        let v = value!(1);
359        let r = Value::from(v.op_add(&i));
360        assert!(r == Value::from(2));
361    }
362}