rbatis_codegen/
ops_xor.rs

1use crate::ops::AsProxy;
2use crate::ops::BitXor;
3use rbs::Value;
4
5fn op_bitxor_value(left: Value, rhs: Value) -> Value {
6    match left {
7        Value::I32(s) => {
8            let rhs = rhs.i32();
9            Value::I32(s ^ rhs)
10        }
11        Value::I64(s) => {
12            let rhs = rhs.i64();
13            Value::I64(s ^ rhs)
14        }
15        Value::U32(s) => {
16            let rhs = rhs.u32();
17            Value::U32(s ^ rhs)
18        }
19        Value::U64(s) => {
20            let rhs = rhs.u64();
21            Value::U64(s ^ rhs)
22        }
23        Value::Ext(_, e) => op_bitxor_value(*e, rhs),
24        _ => Value::Null,
25    }
26}
27
28impl BitXor<Value> for Value {
29    type Output = Value;
30    fn op_bitxor(self, rhs: Value) -> Self::Output {
31        op_bitxor_value(self, rhs)
32    }
33}
34
35//value
36impl BitXor<&Value> for Value {
37    type Output = Value;
38    fn op_bitxor(self, rhs: &Value) -> Self::Output {
39        op_bitxor_value(self, rhs.to_owned())
40    }
41}
42
43impl BitXor<&&Value> for Value {
44    type Output = Value;
45    fn op_bitxor(self, rhs: &&Value) -> Self::Output {
46        op_bitxor_value(self, (*rhs).to_owned())
47    }
48}
49
50impl BitXor<Value> for &Value {
51    type Output = Value;
52    fn op_bitxor(self, rhs: Value) -> Self::Output {
53        op_bitxor_value(self.to_owned(), rhs)
54    }
55}
56
57impl BitXor<&Value> for &Value {
58    type Output = Value;
59    fn op_bitxor(self, rhs: &Value) -> Self::Output {
60        op_bitxor_value(self.to_owned(), rhs.to_owned())
61    }
62}
63
64impl BitXor<&&Value> for &Value {
65    type Output = Value;
66    fn op_bitxor(self, rhs: &&Value) -> Self::Output {
67        op_bitxor_value(self.to_owned(), (*rhs).to_owned())
68    }
69}
70
71fn op_bitxor_i64(value: &Value, other: i64) -> i64 {
72    value.i64() ^ other
73}
74
75fn op_bitxor_f64(_: &Value, _: f64) -> f64 {
76    0.0
77}
78
79fn op_bitxor_i64_value(value: &Value, other: i64) -> i64 {
80    other ^ value.i64()
81}
82
83fn op_bitxor_f64_value(_: &Value, _: f64) -> f64 {
84    0.0
85}
86
87macro_rules! impl_numeric_sub {
88    ($($sub:ident,$sub_value:ident [$($ty:ty)*]-> $return_ty:ty)*) => {
89        $($(
90            impl BitXor<$ty> for Value {
91                type Output = $return_ty;
92                fn op_bitxor(self, other: $ty) -> Self::Output {
93                    $sub(&self, other as _)
94                }
95            }
96
97            impl BitXor<&$ty> for Value {
98                type Output = $return_ty;
99                fn op_bitxor(self, other: &$ty) -> Self::Output {
100                    $sub(&self, *other as _)
101                }
102            }
103
104            impl<'a> BitXor<$ty> for &'a Value {
105                type Output = $return_ty;
106                fn op_bitxor(self, other: $ty) -> Self::Output {
107                    $sub(self, other as _)
108                }
109            }
110
111            impl<'a> BitXor<&$ty> for &'a Value {
112                type Output = $return_ty;
113                fn op_bitxor(self, other: &$ty) -> Self::Output {
114                    $sub(self, *other as _)
115                }
116            }
117
118            impl BitXor<Value> for $ty {
119                type Output = $return_ty;
120                fn op_bitxor(self, other: Value) -> Self::Output {
121                    $sub_value(&other, self as _)
122                }
123            }
124
125            impl BitXor<&Value> for $ty {
126                type Output = $return_ty;
127                fn op_bitxor(self, other: &Value) -> Self::Output {
128                    $sub_value(other, self as _)
129                }
130            }
131
132            impl BitXor<Value> for &$ty {
133                type Output = $return_ty;
134                fn op_bitxor(self, other: Value) -> Self::Output {
135                    $sub_value(&other, *self as _)
136                }
137            }
138
139            impl BitXor<&Value> for &$ty {
140                type Output = $return_ty;
141                fn op_bitxor(self, other: &Value) -> Self::Output {
142                    $sub_value(other, *self as _)
143                }
144            }
145            // for unary
146            impl BitXor<&&Value> for $ty {
147                type Output = $return_ty;
148                fn op_bitxor(self, other: &&Value) -> Self::Output {
149                    $sub_value(other, self as _)
150                }
151            }
152        )*)*
153    }
154}
155
156impl_numeric_sub! {
157    op_bitxor_i64,op_bitxor_i64_value[i8 i16 i32 i64 isize usize] -> i64
158    op_bitxor_f64,op_bitxor_f64_value[f32 f64] -> f64
159}
160
161macro_rules! xor_self {
162([$($ty:ty)*]) => {
163$(
164impl BitXor< $ ty> for $ ty{
165  type Output = $ty;
166  fn op_bitxor(self, rhs: $ty) -> Self::Output {
167      self ^ rhs
168  }
169}
170impl BitXor<& $ ty> for $ ty{
171  type Output = $ty;
172  fn op_bitxor(self, rhs: & $ ty) -> Self::Output {
173    self ^ *rhs
174  }
175}
176impl BitXor< $ ty> for & $ ty{
177  type Output = $ty;
178  fn op_bitxor(self, rhs: $ty) -> Self::Output {
179    *self ^ rhs
180  }
181}
182impl BitXor<& $ ty> for & $ ty{
183  type Output = $ty;
184  fn op_bitxor(self, rhs: & $ ty) -> Self::Output {
185    *self ^ *rhs
186  }
187}
188)*
189};
190}
191
192xor_self!([u8 u16 u32 u64]);
193xor_self!([i8 i16 i32 i64 isize usize]);
194// unsupported! xor_self!([f32 f64]);