rbatis_codegen/
ops_sub.rs1use crate::ops::AsProxy;
2use crate::ops::Sub;
3use rbs::Value;
4
5fn op_sub_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::F32(s) => Value::F32(s - rhs.f64() as f32),
12 Value::F64(s) => Value::F64(s - rhs.f64()),
13 Value::Ext(_, e) => op_sub_value(*e, rhs),
14 _ => Value::Null,
15 }
16}
17
18impl Sub<Value> for Value {
19 type Output = Value;
20 fn op_sub(self, rhs: Value) -> Self::Output {
21 op_sub_value(self, rhs)
22 }
23}
24
25impl Sub<&Value> for Value {
27 type Output = Value;
28 fn op_sub(self, rhs: &Value) -> Self::Output {
29 op_sub_value(self, rhs.to_owned())
30 }
31}
32
33impl Sub<&&Value> for Value {
34 type Output = Value;
35 fn op_sub(self, rhs: &&Value) -> Self::Output {
36 op_sub_value(self, (*rhs).to_owned())
37 }
38}
39
40impl Sub<Value> for &Value {
41 type Output = Value;
42 fn op_sub(self, rhs: Value) -> Self::Output {
43 op_sub_value(self.to_owned(), rhs)
44 }
45}
46
47impl Sub<&Value> for &Value {
48 type Output = Value;
49 fn op_sub(self, rhs: &Value) -> Self::Output {
50 op_sub_value(self.to_owned(), rhs.to_owned())
51 }
52}
53
54impl Sub<&&Value> for &Value {
55 type Output = Value;
56 fn op_sub(self, rhs: &&Value) -> Self::Output {
57 op_sub_value(self.to_owned(), (*rhs).to_owned())
58 }
59}
60
61fn op_sub_u64(value: &Value, other: u64) -> u64 {
62 (value.u64() - other) as u64
63}
64
65fn op_sub_i64(value: &Value, other: i64) -> i64 {
66 value.i64() - other
67}
68
69fn op_sub_f64(value: &Value, other: f64) -> f64 {
70 value.f64() - other
71}
72
73fn op_sub_u64_value(value: &Value, other: u64) -> u64 {
74 (other - value.u64()) as u64
75}
76
77fn op_sub_i64_value(value: &Value, other: i64) -> i64 {
78 other - value.i64()
79}
80
81fn op_sub_f64_value(value: &Value, other: f64) -> f64 {
82 other - value.f64()
83}
84
85macro_rules! impl_numeric_sub {
86 ($($sub:ident,$sub_value:ident [$($ty:ty)*]-> $return_ty:ty)*) => {
87 $($(
88 impl Sub<$ty> for Value {
89 type Output = $return_ty;
90 fn op_sub(self, other: $ty) -> Self::Output {
91 $sub(&self, other as _)
92 }
93 }
94
95 impl Sub<&$ty> for Value {
96 type Output = $return_ty;
97 fn op_sub(self, other: &$ty) -> Self::Output {
98 $sub(&self, *other as _)
99 }
100 }
101
102 impl<'a> Sub<$ty> for &'a Value {
103 type Output = $return_ty;
104 fn op_sub(self, other: $ty) -> Self::Output {
105 $sub(self, other as _)
106 }
107 }
108
109 impl<'a> Sub<&$ty> for &'a Value {
110 type Output = $return_ty;
111 fn op_sub(self, other: &$ty) -> Self::Output {
112 $sub(self, *other as _)
113 }
114 }
115
116 impl Sub<Value> for $ty {
117 type Output = $return_ty;
118 fn op_sub(self, other: Value) -> Self::Output {
119 $sub_value(&other, self as _)
120 }
121 }
122
123 impl Sub<&Value> for $ty {
124 type Output = $return_ty;
125 fn op_sub(self, other: &Value) -> Self::Output {
126 $sub_value(other, self as _)
127 }
128 }
129
130 impl Sub<Value> for &$ty {
131 type Output = $return_ty;
132 fn op_sub(self, other: Value) -> Self::Output {
133 $sub_value(&other, *self as _)
134 }
135 }
136
137 impl Sub<&Value> for &$ty {
138 type Output = $return_ty;
139 fn op_sub(self, other: &Value) -> Self::Output {
140 $sub_value(other, *self as _)
141 }
142 }
143 impl Sub<&&Value> for $ty {
145 type Output = $return_ty;
146 fn op_sub(self, other: &&Value) -> Self::Output {
147 $sub_value(*other, self as _)
148 }
149 }
150 )*)*
151 }
152}
153
154impl_numeric_sub! {
155 op_sub_u64,op_sub_u64_value[u8 u16 u32 u64] -> u64
156 op_sub_i64,op_sub_i64_value[i8 i16 i32 i64 isize usize] -> i64
157 op_sub_f64,op_sub_f64_value[f32 f64] -> f64
158}
159
160macro_rules! self_sub {
161 ([$($ty:ty)*]) => {
162 $(
163impl Sub<$ty> for $ty{
164 type Output = $ty;
165 fn op_sub(self, rhs: $ty) -> Self::Output {
166 self-rhs
167 }
168}
169impl Sub<&$ty> for $ty{
170 type Output = $ty;
171 fn op_sub(self, rhs: &$ty) -> Self::Output {
172 self-*rhs
173 }
174}
175impl Sub<$ty> for &$ty{
176 type Output = $ty;
177 fn op_sub(self, rhs: $ty) -> Self::Output {
178 *self-rhs
179 }
180}
181impl Sub<&$ty> for &$ty{
182 type Output = $ty;
183 fn op_sub(self, rhs: &$ty) -> Self::Output {
184 *self-*rhs
185 }
186}
187 )*
188 };
189}
190
191self_sub!([u8 u16 u32 u64]);
192self_sub!([i8 i16 i32 i64 isize usize]);
193self_sub!([f32 f64]);
194
195#[cfg(test)]
196mod test {
197 use crate::ops::Sub;
198 use rbs::Value;
199
200 #[test]
201 fn test_sub() {
202 let v = 0.op_sub(&&Value::I32(-1));
203 println!("{}", v);
204 assert_eq!(v, 1i64);
205 }
206}