Skip to main content

rbatis_codegen/
ops_shl.rs

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