mybatis_sql/
ops_sub.rs

1use crate::ops::AsProxy;
2use crate::ops::Sub;
3use crate::ops::Value;
4
5//value
6impl Sub<&Value> for Value {
7    type Output = Value;
8    fn op_sub(self, rhs: &Value) -> Self::Output {
9        match self {
10            Value::Int32(s) => {
11                let rhs = rhs.i32();
12                return Value::Int64((s - rhs) as i64);
13            }
14            Value::Int64(s) => {
15                let rhs = rhs.i64();
16                return Value::Int64(s - rhs);
17            }
18            Value::UInt32(s) => {
19                let rhs = rhs.u32();
20                return Value::UInt64((s - rhs) as u64);
21            }
22            Value::UInt64(s) => {
23                let rhs = rhs.u64();
24                return Value::UInt64(s - rhs);
25            }
26            Value::Double(s) => {
27                let rhs = rhs.as_f64().unwrap_or_default();
28                return Value::Double(s - rhs);
29            }
30            _ => {
31                return Value::Null;
32            }
33        };
34    }
35}
36
37impl Sub<&&Value> for Value {
38    type Output = Value;
39    fn op_sub(self, rhs: &&Value) -> Self::Output {
40        match self {
41            Value::Int32(s) => {
42                let rhs = rhs.i32();
43                return Value::Int64((s - rhs) as i64);
44            }
45            Value::Int64(s) => {
46                let rhs = rhs.i64();
47                return Value::Int64(s - rhs);
48            }
49            Value::UInt32(s) => {
50                let rhs = rhs.u32();
51                return Value::UInt64((s - rhs) as u64);
52            }
53            Value::UInt64(s) => {
54                let rhs = rhs.u64();
55                return Value::UInt64(s - rhs);
56            }
57            Value::Double(s) => {
58                let rhs = rhs.as_f64().unwrap_or_default();
59                return Value::Double(s - rhs);
60            }
61            _ => {
62                return Value::Null;
63            }
64        };
65    }
66}
67
68impl Sub<Value> for Value {
69    type Output = Value;
70    fn op_sub(self, rhs: Value) -> Self::Output {
71        match self {
72            Value::Int32(s) => {
73                let rhs = rhs.i32();
74                return Value::Int64((s - rhs) as i64);
75            }
76            Value::Int64(s) => {
77                let rhs = rhs.i64();
78                return Value::Int64(s - rhs);
79            }
80            Value::UInt32(s) => {
81                let rhs = rhs.u32();
82                return Value::UInt64((s - rhs) as u64);
83            }
84            Value::UInt64(s) => {
85                let rhs = rhs.u64();
86                return Value::UInt64(s - rhs);
87            }
88            Value::Double(s) => {
89                let rhs = rhs.as_f64().unwrap_or_default();
90                return Value::Double(s - rhs);
91            }
92            _ => {
93                return Value::Null;
94            }
95        };
96    }
97}
98
99impl Sub<&Value> for &Value {
100    type Output = Value;
101    fn op_sub(self, rhs: &Value) -> Self::Output {
102        match self {
103            Value::Int32(s) => {
104                let rhs = rhs.i32();
105                return Value::Int64((s - rhs) as i64);
106            }
107            Value::Int64(s) => {
108                let rhs = rhs.i64();
109                return Value::Int64(s - rhs);
110            }
111            Value::UInt32(s) => {
112                let rhs = rhs.u32();
113                return Value::UInt64((s - rhs) as u64);
114            }
115            Value::UInt64(s) => {
116                let rhs = rhs.u64();
117                return Value::UInt64(s - rhs);
118            }
119            Value::Double(s) => {
120                let rhs = rhs.as_f64().unwrap_or_default();
121                return Value::Double(s - rhs);
122            }
123            _ => {
124                return Value::Null;
125            }
126        };
127    }
128}
129
130impl Sub<&&Value> for &Value {
131    type Output = Value;
132    fn op_sub(self, rhs: &&Value) -> Self::Output {
133        match self {
134            Value::Int32(s) => {
135                let rhs = rhs.i32();
136                return Value::Int64((s - rhs) as i64);
137            }
138            Value::Int64(s) => {
139                let rhs = rhs.i64();
140                return Value::Int64(s - rhs);
141            }
142            Value::UInt32(s) => {
143                let rhs = rhs.u32();
144                return Value::UInt64((s - rhs) as u64);
145            }
146            Value::UInt64(s) => {
147                let rhs = rhs.u64();
148                return Value::UInt64(s - rhs);
149            }
150            Value::Double(s) => {
151                let rhs = rhs.as_f64().unwrap_or_default();
152                return Value::Double(s - rhs);
153            }
154            _ => {
155                return Value::Null;
156            }
157        };
158    }
159}
160
161impl Sub<Value> for &Value {
162    type Output = Value;
163    fn op_sub(self, rhs: Value) -> Self::Output {
164        match self {
165            Value::Int32(s) => {
166                let rhs = rhs.i32();
167                return Value::Int64((s - rhs) as i64);
168            }
169            Value::Int64(s) => {
170                let rhs = rhs.i64();
171                return Value::Int64(s - rhs);
172            }
173            Value::UInt32(s) => {
174                let rhs = rhs.u32();
175                return Value::UInt64((s - rhs) as u64);
176            }
177            Value::UInt64(s) => {
178                let rhs = rhs.u64();
179                return Value::UInt64(s - rhs);
180            }
181            Value::Double(s) => {
182                let rhs = rhs.as_f64().unwrap_or_default();
183                return Value::Double(s - rhs);
184            }
185            _ => {
186                return Value::Null;
187            }
188        };
189    }
190}
191
192fn op_sub_u64(value: &Value, other: u64) -> u64 {
193    (value.u64() - other) as u64
194}
195
196fn op_sub_i64(value: &Value, other: i64) -> i64 {
197    value.i64() - other
198}
199
200fn op_sub_f64(value: &Value, other: f64) -> f64 {
201    value.f64() - other
202}
203
204fn op_sub_u64_value(value: &Value, other: u64) -> u64 {
205    (other - value.u64()) as u64
206}
207
208fn op_sub_i64_value(value: &Value, other: i64) -> i64 {
209    other - value.i64()
210}
211
212fn op_sub_f64_value(value: &Value, other: f64) -> f64 {
213    other - value.f64()
214}
215
216macro_rules! impl_numeric_sub {
217    ($($sub:ident,$sub_value:ident [$($ty:ty)*]-> $return_ty:ty)*) => {
218        $($(
219            impl Sub<$ty> for Value {
220                type Output = $return_ty;
221                fn op_sub(self, other: $ty) -> Self::Output {
222                    $sub(&self, other as _)
223                }
224            }
225
226            impl Sub<Value> for $ty {
227                type Output = $return_ty;
228                fn op_sub(self, other: Value) -> Self::Output {
229                    $sub_value(&other, self as _)
230                }
231            }
232
233            impl Sub<&Value> for $ty {
234                type Output = $return_ty;
235                fn op_sub(self, other: &Value) -> Self::Output {
236                    $sub_value(other, self as _)
237                }
238            }
239            impl Sub<&&Value> for $ty {
240                type Output = $return_ty;
241                fn op_sub(self, other: &&Value) -> Self::Output {
242                    $sub_value(*other, self as _)
243                }
244            }
245
246            impl<'a> Sub<$ty> for &'a Value {
247                type Output = $return_ty;
248                fn op_sub(self, other: $ty) -> Self::Output {
249                    $sub(self, other as _)
250                }
251            }
252        )*)*
253    }
254}
255
256impl_numeric_sub! {
257    op_sub_u64,op_sub_u64_value[u8 u16 u32 u64] -> u64
258    op_sub_i64,op_sub_i64_value[i8 i16 i32 i64 isize] -> i64
259    op_sub_f64,op_sub_f64_value[f32 f64] -> f64
260}
261
262macro_rules! sub_self {
263    ([$($ty:ty)*]) => {
264        $(
265impl Sub<$ty> for $ty{
266         type Output = $ty;
267      fn op_sub(self, rhs: $ty) -> Self::Output {
268        self-rhs
269      }
270    }
271impl Sub<&$ty> for $ty{
272         type Output = $ty;
273      fn op_sub(self, rhs: &$ty) -> Self::Output {
274        self-*rhs
275      }
276    }
277impl Sub<$ty> for &$ty{
278         type Output = $ty;
279      fn op_sub(self, rhs: $ty) -> Self::Output {
280        *self-rhs
281      }
282    }
283impl Sub<&$ty> for &$ty{
284         type Output = $ty;
285      fn op_sub(self, rhs: &$ty) -> Self::Output {
286        *self-*rhs
287      }
288    }
289        )*
290    };
291}
292
293sub_self!([i8 i16 i32 i64 isize]);
294sub_self!([f32 f64]);