mybatis_sql/
ops_xor.rs

1use crate::ops::AsProxy;
2use crate::ops::BitXor;
3use crate::ops::Value;
4
5//value
6impl BitXor<&Value> for Value {
7    type Output = Value;
8    fn op_bitxor(self, rhs: &Value) -> Self::Output {
9        match self {
10            Value::Int32(s) => {
11                let rhs = rhs.i32();
12                return Value::Int32(s ^ rhs);
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::UInt32(s ^ rhs);
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::Null;
29            }
30            _ => {
31                return Value::Null;
32            }
33        };
34    }
35}
36
37impl BitXor<&&Value> for Value {
38    type Output = Value;
39    fn op_bitxor(self, rhs: &&Value) -> Self::Output {
40        match self {
41            Value::Int32(s) => {
42                let rhs = rhs.i32();
43                return Value::Int32(s ^ rhs);
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::UInt32(s ^ rhs);
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::Null;
60            }
61            _ => {
62                return Value::Null;
63            }
64        };
65    }
66}
67
68impl BitXor<Value> for Value {
69    type Output = Value;
70    fn op_bitxor(self, rhs: Value) -> Self::Output {
71        match self {
72            Value::Int32(s) => {
73                let rhs = rhs.i32();
74                return Value::Int32(s ^ rhs);
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::UInt32(s ^ rhs);
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::Null;
91            }
92            _ => {
93                return Value::Null;
94            }
95        };
96    }
97}
98
99impl BitXor<&Value> for &Value {
100    type Output = Value;
101    fn op_bitxor(self, rhs: &Value) -> Self::Output {
102        match self {
103            Value::Int32(s) => {
104                let rhs = rhs.i32();
105                return Value::Int32(s ^ rhs);
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::UInt32(s ^ rhs);
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::Null;
122            }
123            _ => {
124                return Value::Null;
125            }
126        };
127    }
128}
129
130impl BitXor<&&Value> for &Value {
131    type Output = Value;
132    fn op_bitxor(self, rhs: &&Value) -> Self::Output {
133        match self {
134            Value::Int32(s) => {
135                let rhs = rhs.i32();
136                return Value::Int32(s ^ rhs);
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::UInt32(s ^ rhs);
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::Null;
153            }
154            _ => {
155                return Value::Null;
156            }
157        };
158    }
159}
160
161impl BitXor<Value> for &Value {
162    type Output = Value;
163    fn op_bitxor(self, rhs: Value) -> Self::Output {
164        match self {
165            Value::Int32(s) => {
166                let rhs = rhs.i32();
167                return Value::Int32(s ^ rhs);
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::UInt32(s ^ rhs);
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::Null;
184            }
185            _ => {
186                return Value::Null;
187            }
188        };
189    }
190}
191
192fn op_bitxor_u64(value: &Value, other: u64) -> u64 {
193    value.u64() ^ other
194}
195
196fn op_bitxor_i64(value: &Value, other: i64) -> i64 {
197    value.i64() ^ other
198}
199
200fn op_bitxor_f64(value: &Value, other: f64) -> f64 {
201    0.0
202}
203
204fn op_bitxor_u64_value(value: &Value, other: u64) -> u64 {
205    other ^ value.u64()
206}
207
208fn op_bitxor_i64_value(value: &Value, other: i64) -> i64 {
209    other ^ value.i64()
210}
211
212fn op_bitxor_f64_value(value: &Value, other: f64) -> f64 {
213    0.0
214}
215
216macro_rules! impl_numeric_sub {
217    ($($sub:ident,$sub_value:ident [$($ty:ty)*]-> $return_ty:ty)*) => {
218        $($(
219            impl BitXor<$ty> for Value {
220                type Output = $return_ty;
221                fn op_bitxor(self, other: $ty) -> Self::Output {
222                    $sub(&self, other as _)
223                }
224            }
225
226            impl BitXor<Value> for $ty {
227                type Output = $return_ty;
228                fn op_bitxor(self, other: Value) -> Self::Output {
229                    $sub_value(&other, self as _)
230                }
231            }
232
233            impl BitXor<&Value> for $ty {
234                type Output = $return_ty;
235                fn op_bitxor(self, other: &Value) -> Self::Output {
236                    $sub_value(other, self as _)
237                }
238            }
239            impl BitXor<&&Value> for $ty {
240                type Output = $return_ty;
241                fn op_bitxor(self, other: &&Value) -> Self::Output {
242                    $sub_value(*other, self as _)
243                }
244            }
245
246            impl<'a> BitXor<$ty> for &'a Value {
247                type Output = $return_ty;
248                fn op_bitxor(self, other: $ty) -> Self::Output {
249                    $sub(self, other as _)
250                }
251            }
252        )*)*
253    }
254}
255
256impl_numeric_sub! {
257    op_bitxor_i64,op_bitxor_i64_value[i8 i16 i32 i64 isize] -> i64
258    op_bitxor_f64,op_bitxor_f64_value[f32 f64] -> f64
259}
260
261macro_rules! xor_self {
262    ([$($ty:ty)*]) => {
263        $(
264impl BitXor<$ty> for $ty{
265         type Output = $ty;
266      fn op_bitxor(self, rhs: $ty) -> Self::Output {
267        self ^ rhs
268      }
269    }
270impl BitXor<&$ty> for $ty{
271         type Output = $ty;
272      fn op_bitxor(self, rhs: &$ty) -> Self::Output {
273        self ^ *rhs
274      }
275    }
276impl BitXor<$ty> for &$ty{
277         type Output = $ty;
278      fn op_bitxor(self, rhs: $ty) -> Self::Output {
279        *self ^ rhs
280      }
281    }
282impl BitXor<&$ty> for &$ty{
283         type Output = $ty;
284      fn op_bitxor(self, rhs: &$ty) -> Self::Output {
285        *self ^ *rhs
286      }
287    }
288        )*
289    };
290}
291
292xor_self!([i8 i16 i32 i64 isize]);
293// unsupport! xor_self!([f32 f64]);