rbatis_codegen/
ops_mul.rs

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