mybatis_sql/
ops_add.rs

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