mybatis_sql/
ops_div.rs

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