rbatis_codegen/
ops_div.rs1use crate::ops::AsProxy;
2use crate::ops::Div;
3use rbs::Value;
4
5fn op_div_u64(value: &Value, other: u64) -> u64 {
6 if other == 0 {
7 return 0;
8 }
9 value.u64() / other
10}
11
12fn op_div_i64(value: &Value, other: i64) -> i64 {
13 if other == 0 {
14 return 0;
15 }
16 value.i64() / other
17}
18
19fn op_div_f64(value: &Value, other: f64) -> f64 {
20 if other == 0.0 {
21 0.0
22 } else {
23 value.f64() / other
24 }
25}
26
27fn op_div_i64_value(value: &Value, other: i64) -> i64 {
28 let v = value.i64();
29 if v == 0 {
30 return 0;
31 }
32 other / v
33}
34
35fn op_div_u64_value(value: &Value, other: u64) -> u64 {
36 let v = value.u64();
37 if v == 0 {
38 return 0;
39 }
40 other / v
41}
42
43fn op_div_f64_value(value: &Value, other: f64) -> f64 {
44 let v = value.f64();
45 if v == 0.0 {
46 0.0
47 } else {
48 other / v
49 }
50}
51
52macro_rules! impl_numeric_div {
53 ($($div:ident,$div_value:ident [$($ty:ty)*]-> $return_ty:ty)*) => {
54 $($(
55 impl Div<$ty> for Value {
56 type Output = $return_ty;
57 fn op_div(self, other: $ty) -> Self::Output {
58 $div(&self, other as _)
59 }
60 }
61
62 impl Div<&$ty> for Value {
63 type Output = $return_ty;
64 fn op_div(self, other: &$ty) -> Self::Output {
65 $div(&self, *other as _)
66 }
67 }
68
69 impl<'a> Div<$ty> for &'a Value {
70 type Output = $return_ty;
71 fn op_div(self, other: $ty) -> Self::Output {
72 $div(self, other as _)
73 }
74 }
75
76 impl<'a> Div<&$ty> for &'a Value {
77 type Output = $return_ty;
78 fn op_div(self, other: &$ty) -> Self::Output {
79 $div(self, *other as _)
80 }
81 }
82
83 impl Div<Value> for $ty {
84 type Output = $return_ty;
85 fn op_div(self, other: Value) -> Self::Output {
86 $div_value(&other, self as _)
87 }
88 }
89
90 impl Div<&Value> for $ty {
91 type Output = $return_ty;
92 fn op_div(self, other: &Value) -> Self::Output {
93 $div_value(other, self as _)
94 }
95 }
96
97 impl Div<Value> for &$ty {
98 type Output = $return_ty;
99 fn op_div(self, other: Value) -> Self::Output {
100 $div_value(&other, *self as _)
101 }
102 }
103
104 impl Div<&Value> for &$ty {
105 type Output = $return_ty;
106 fn op_div(self, other: &Value) -> Self::Output {
107 $div_value(other, *self as _)
108 }
109 }
110
111 impl Div<&&Value> for $ty {
113 type Output = $return_ty;
114 fn op_div(self, other: &&Value) -> Self::Output {
115 $div_value(other, self as _)
116 }
117 }
118 )*)*
119 }
120}
121
122impl_numeric_div! {
123 op_div_u64,op_div_u64_value[u8 u16 u32 u64] -> u64
124 op_div_i64,op_div_i64_value[i8 i16 i32 i64 isize usize] -> i64
125 op_div_f64,op_div_f64_value[f32 f64] -> f64
126}
127
128fn op_div_value(left: Value, rhs: Value) -> Value {
129 match left {
130 Value::I32(s) => {
131 let rhs = rhs.f64();
132 if rhs == 0.0 {
133 return Value::I32(0);
134 }
135 Value::I32((s as f64 / rhs) as i32)
136 }
137 Value::I64(s) => {
138 let rhs = rhs.f64();
139 if rhs == 0.0 {
140 return Value::I64(0);
141 }
142 Value::I64((s as f64 / rhs) as i64)
143 }
144 Value::U32(s) => {
145 let rhs = rhs.u64();
146 if rhs == 0 {
147 return Value::U32(0);
148 }
149 Value::U32((s as f64 / rhs as f64) as u32)
150 }
151 Value::U64(s) => {
152 let rhs = rhs.u64();
153 if rhs == 0 {
154 return Value::U64(0);
155 }
156 Value::U64((s as f64 / rhs as f64) as u64)
157 }
158 Value::F32(s) => {
159 let rhs = rhs.f64() as f32;
160 if rhs == 0.0 {
161 return Value::F32(0.0);
162 }
163 Value::F32(s / rhs)
164 }
165 Value::F64(s) => {
166 let rhs = rhs.f64();
167 if rhs == 0.0 {
168 return Value::F64(0.0);
169 }
170 Value::F64(s / rhs)
171 }
172 Value::Ext(_, e) => op_div_value(*e, rhs),
173 _ => Value::Null,
174 }
175}
176
177impl Div<Value> for Value {
179 type Output = Value;
180 fn op_div(self, rhs: Value) -> Self::Output {
181 op_div_value(self, rhs)
182 }
183}
184
185impl Div<&Value> for Value {
186 type Output = Value;
187 fn op_div(self, rhs: &Value) -> Self::Output {
188 op_div_value(self, rhs.to_owned())
189 }
190}
191
192impl Div<&&Value> for Value {
193 type Output = Value;
194 fn op_div(self, rhs: &&Value) -> Self::Output {
195 op_div_value(self, (*rhs).to_owned())
196 }
197}
198
199impl Div<Value> for &Value {
200 type Output = Value;
201 fn op_div(self, rhs: Value) -> Self::Output {
202 op_div_value(self.to_owned(), rhs.to_owned())
203 }
204}
205
206impl Div<&Value> for &Value {
207 type Output = Value;
208 fn op_div(self, rhs: &Value) -> Self::Output {
209 op_div_value(self.to_owned(), rhs.to_owned())
210 }
211}
212
213impl Div<&&Value> for &Value {
214 type Output = Value;
215 fn op_div(self, rhs: &&Value) -> Self::Output {
216 op_div_value(self.to_owned(), (*rhs).to_owned())
217 }
218}
219
220impl Div<&&Value> for &&Value {
221 type Output = Value;
222 fn op_div(self, rhs: &&Value) -> Self::Output {
223 op_div_value((*self).to_owned(), (*rhs).to_owned())
224 }
225}
226
227macro_rules! self_div {
228 ([$($ty:ty)*]) => {
229 $(
230impl Div<$ty> for $ty{
231 type Output = $ty;
232 fn op_div(self, rhs: $ty) -> Self::Output {
233 self / rhs
234 }
235}
236impl Div<&$ty> for $ty{
237 type Output = $ty;
238 fn op_div(self, rhs: &$ty) -> Self::Output {
239 self / *rhs
240 }
241}
242impl Div<$ty> for &$ty{
243 type Output = $ty;
244 fn op_div(self, rhs: $ty) -> Self::Output {
245 *self / rhs
246 }
247}
248impl Div<&$ty> for &$ty{
249 type Output = $ty;
250 fn op_div(self, rhs: &$ty) -> Self::Output {
251 *self / *rhs
252 }
253}
254 )*
255 };
256}
257self_div!([u8 u16 u32 u64]);
258self_div!([i8 i16 i32 i64 isize usize]);
259self_div!([f32 f64]);