rbatis_codegen/
ops_add.rs1use crate::ops::{Add, AsProxy};
2use rbs::Value;
3
4fn op_add_u64(value: &Value, other: u64) -> u64 {
5 value.u64() + other
6}
7
8fn op_add_i64(value: &Value, other: i64) -> i64 {
9 value.i64() + other
10}
11
12fn op_add_f64(value: &Value, other: f64) -> f64 {
13 value.f64() + other
14}
15
16macro_rules! impl_numeric_add {
17 ($($eq:ident [$($ty:ty)*]-> $return_ty:ty)*) => {
18 $($(
19 impl Add<$ty> for Value {
20 type Output = $return_ty;
21 fn op_add(self, other: $ty) -> Self::Output {
22 $eq(&self, other as _)
23 }
24 }
25
26 impl Add<&$ty> for Value {
27 type Output = $return_ty;
28 fn op_add(self, other: &$ty) -> Self::Output {
29 $eq(&self, *other as _)
30 }
31 }
32
33 impl<'a> Add<$ty> for &'a Value {
34 type Output = $return_ty;
35 fn op_add(self, other: $ty) -> Self::Output {
36 $eq(self, other as _)
37 }
38 }
39
40 impl<'a> Add<&$ty> for &'a Value {
41 type Output = $return_ty;
42 fn op_add(self, other: &$ty) -> Self::Output {
43 $eq(self, *other as _)
44 }
45 }
46
47 impl Add<Value> for $ty {
48 type Output = $return_ty;
49 fn op_add(self, other: Value) -> Self::Output {
50 $eq(&other, self as _)
51 }
52 }
53
54 impl Add<&Value> for $ty {
55 type Output = $return_ty;
56 fn op_add(self, other: &Value) -> Self::Output {
57 $eq(other, self as _)
58 }
59 }
60
61 impl Add<Value> for &$ty {
62 type Output = $return_ty;
63 fn op_add(self, other: Value) -> Self::Output {
64 $eq(&other, *self as _)
65 }
66 }
67
68 impl Add<&Value> for &$ty {
69 type Output = $return_ty;
70 fn op_add(self, other: &Value) -> Self::Output {
71 $eq(other, *self as _)
72 }
73 }
74
75 impl Add<&&Value> for $ty {
77 type Output = $return_ty;
78 fn op_add(self, other: &&Value) -> Self::Output {
79 $eq(other, self as _)
80 }
81 }
82 )*)*
83 }
84}
85
86impl_numeric_add! {
87 op_add_u64[u8 u16 u32 u64] -> u64
88 op_add_i64[i8 i16 i32 i64 isize usize] -> i64
89 op_add_f64[f32 f64] -> f64
90}
91
92fn op_add_value(left: Value, rhs: &Value) -> Value {
93 match left {
94 Value::String(s) => Value::String(s + rhs.as_str().unwrap_or("")),
95 Value::I32(s) => Value::I32(s + rhs.i32()),
96 Value::I64(s) => Value::I64(s + rhs.i64()),
97 Value::U32(s) => Value::U32(s + rhs.u32()),
98 Value::U64(s) => Value::U64(s + rhs.u64()),
99 Value::F64(s) => Value::F64(s + rhs.f64()),
100 Value::F32(v) => Value::F64(v as f64 + rhs.f64()),
101 Value::Ext(_, e) => op_add_value(*e, rhs),
102 _ => Value::Null,
103 }
104}
105
106impl Add<&Value> for Value {
108 type Output = Value;
109 fn op_add(self, rhs: &Value) -> Self::Output {
110 op_add_value(self, rhs)
111 }
112}
113
114impl Add<&&Value> for Value {
115 type Output = Value;
116 fn op_add(self, rhs: &&Value) -> Self::Output {
117 op_add_value(self, rhs)
118 }
119}
120
121impl Add<Value> for Value {
122 type Output = Value;
123 fn op_add(self, rhs: Value) -> Self::Output {
124 op_add_value(self, &rhs)
125 }
126}
127
128impl Add<&Value> for &Value {
129 type Output = Value;
130 fn op_add(self, rhs: &Value) -> Self::Output {
131 op_add_value(self.to_owned(), rhs)
132 }
133}
134
135impl Add<&&Value> for &Value {
136 type Output = Value;
137 fn op_add(self, rhs: &&Value) -> Self::Output {
138 op_add_value(self.to_owned(), rhs)
139 }
140}
141
142impl Add<Value> for &Value {
143 type Output = Value;
144 fn op_add(self, rhs: Value) -> Self::Output {
145 op_add_value(self.to_owned(), &rhs)
146 }
147}
148
149impl Add<Value> for &str {
151 type Output = String;
152 fn op_add(self, rhs: Value) -> Self::Output {
153 self.to_string() + &rhs.string()
154 }
155}
156
157impl Add<&Value> for &str {
158 type Output = String;
159 fn op_add(self, rhs: &Value) -> Self::Output {
160 self.to_string() + rhs.clone().string().as_str()
161 }
162}
163
164impl Add<&str> for Value {
165 type Output = Value;
166 fn op_add(self, rhs: &str) -> Self::Output {
167 Value::String(self.string() + rhs)
168 }
169}
170
171impl Add<&str> for &Value {
172 type Output = Value;
173 fn op_add(self, rhs: &str) -> Self::Output {
174 Value::String(self.to_owned().string() + rhs)
175 }
176}
177
178impl Add<&&str> for Value {
179 type Output = Value;
180 fn op_add(self, rhs: &&str) -> Self::Output {
181 Value::String(self.string() + rhs)
182 }
183}
184
185impl Add<&&str> for &Value {
186 type Output = Value;
187 fn op_add(self, rhs: &&str) -> Self::Output {
188 Value::String(self.to_owned().string() + rhs)
189 }
190}
191
192impl Add<String> for Value {
193 type Output = Value;
194 fn op_add(self, rhs: String) -> Self::Output {
195 Value::String(self.string() + rhs.as_str())
196 }
197}
198
199impl Add<String> for &Value {
200 type Output = Value;
201 fn op_add(self, rhs: String) -> Self::Output {
202 Value::String(self.to_owned().string() + rhs.as_str())
203 }
204}
205
206impl Add<Value> for &String {
207 type Output = String;
208 fn op_add(self, rhs: Value) -> Self::Output {
209 self.to_string() + &rhs.string()
210 }
211}
212
213impl Add<&Value> for &String {
214 type Output = String;
215 fn op_add(self, rhs: &Value) -> Self::Output {
216 self.to_string() + &rhs.clone().string()
217 }
218}
219
220impl Add<&String> for Value {
221 type Output = Value;
222 fn op_add(self, rhs: &String) -> Self::Output {
223 Value::String(self.string() + rhs.as_str())
224 }
225}
226
227impl Add<&String> for &Value {
228 type Output = Value;
229 fn op_add(self, rhs: &String) -> Self::Output {
230 Value::String(self.to_owned().string() + rhs.as_str())
231 }
232}
233
234impl Add<Value> for String {
235 type Output = String;
236 fn op_add(self, rhs: Value) -> Self::Output {
237 self.to_string() + &rhs.string()
238 }
239}
240
241impl Add<&Value> for String {
242 type Output = String;
243 fn op_add(self, rhs: &Value) -> Self::Output {
244 self.to_string() + &rhs.clone().string()
245 }
246}
247
248impl Add<&&Value> for String {
249 type Output = String;
250
251 fn op_add(self, rhs: &&Value) -> Self::Output {
252 self + &rhs.string()
253 }
254}
255
256macro_rules! self_add {
257 ([$($ty:ty)*]) => {
258 $(
259 impl Add<$ty> for $ty{
260 type Output = $ty;
261 fn op_add(self, rhs: $ty) -> Self::Output {
262 self+rhs
263 }
264 }
265 impl Add<&$ty> for $ty{
266 type Output = $ty;
267 fn op_add(self, rhs: &$ty) -> Self::Output {
268 self+*rhs
269 }
270 }
271 impl Add<$ty> for &$ty{
272 type Output = $ty;
273 fn op_add(self, rhs: $ty) -> Self::Output {
274 *self+rhs
275 }
276 }
277 impl Add<&$ty> for &$ty{
278 type Output = $ty;
279 fn op_add(self, rhs: &$ty) -> Self::Output {
280 *self+*rhs
281 }
282 }
283 )*
284 };
285}
286self_add!([u8 u16 u32 u64]);
287self_add!([i8 i16 i32 i64 isize usize]);
288self_add!([f32 f64]);
289
290impl Add<String> for String {
291 type Output = String;
292
293 fn op_add(self, rhs: String) -> Self::Output {
294 self + &rhs
295 }
296}
297
298impl Add<&str> for String {
299 type Output = String;
300
301 fn op_add(self, rhs: &str) -> Self::Output {
302 self + rhs
303 }
304}
305
306impl Add<&&str> for String {
307 type Output = String;
308
309 fn op_add(self, rhs: &&str) -> Self::Output {
310 self + *rhs
311 }
312}
313
314impl Add<String> for &str {
315 type Output = String;
316
317 fn op_add(self, rhs: String) -> Self::Output {
318 self.to_string() + &rhs
319 }
320}
321
322impl Add<&String> for &str {
323 type Output = String;
324
325 fn op_add(self, rhs: &String) -> Self::Output {
326 self.to_string() + rhs.as_str()
327 }
328}
329
330impl Add<&&String> for &str {
331 type Output = String;
332
333 fn op_add(self, rhs: &&String) -> Self::Output {
334 self.to_string() + rhs.as_str()
335 }
336}
337
338#[cfg(test)]
339mod test {
340 use crate::ops::Add;
341 use rbs::{value, Value};
342
343 #[test]
344 fn test_add() {
345 let i: i64 = 1;
346 let v = value!(1);
347 let r = Value::from(v.op_add(&i));
348 assert_eq!(r, Value::from(2));
349 }
350}