multiversx_sc/types/managed/wrapped/
big_uint_operators.rs1use crate::{
2 api::{const_handles, BigIntApiImpl, ManagedTypeApi},
3 types::{BigUint, ManagedType},
4};
5use core::ops::{
6 Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
7 Mul, MulAssign, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
8};
9
10macro_rules! binary_operator {
11 ($trait:ident, $method:ident, $api_func:ident) => {
12 impl<M: ManagedTypeApi> $trait<Self> for BigUint<M> {
13 type Output = BigUint<M>;
14
15 fn $method(self, other: BigUint<M>) -> BigUint<M> {
16 M::managed_type_impl().$api_func(
17 self.get_handle(),
18 self.get_handle(),
19 other.get_handle(),
20 );
21 self
22 }
23 }
24
25 impl<'a, 'b, M: ManagedTypeApi> $trait<&'b BigUint<M>> for &'a BigUint<M> {
26 type Output = BigUint<M>;
27
28 fn $method(self, other: &BigUint<M>) -> BigUint<M> {
29 unsafe {
30 let result = BigUint::new_uninit();
31 M::managed_type_impl().$api_func(
32 result.get_handle(),
33 self.get_handle(),
34 other.get_handle(),
35 );
36 result
37 }
38 }
39 }
40
41 impl<'b, M: ManagedTypeApi> $trait<&'b BigUint<M>> for BigUint<M> {
42 type Output = BigUint<M>;
43
44 fn $method(self, other: &BigUint<M>) -> BigUint<M> {
45 M::managed_type_impl().$api_func(
46 self.get_handle(),
47 self.get_handle(),
48 other.get_handle(),
49 );
50 self
51 }
52 }
53
54 impl<M: ManagedTypeApi> $trait<u32> for BigUint<M> {
55 type Output = BigUint<M>;
56
57 fn $method(self, other: u32) -> BigUint<M> {
58 let big_int_temp_1 = Self::make_temp(const_handles::BIG_INT_TEMPORARY_1, other);
59 M::managed_type_impl().$api_func(
60 self.get_handle(),
61 self.get_handle(),
62 big_int_temp_1,
63 );
64 self
65 }
66 }
67
68 impl<'a, M: ManagedTypeApi> $trait<u32> for &'a BigUint<M> {
69 type Output = BigUint<M>;
70
71 fn $method(self, other: u32) -> BigUint<M> {
72 let big_int_temp_1 =
73 BigUint::<M>::make_temp(const_handles::BIG_INT_TEMPORARY_1, other);
74 unsafe {
75 let result = BigUint::new_uninit();
76 M::managed_type_impl().$api_func(
77 result.get_handle(),
78 self.get_handle(),
79 big_int_temp_1,
80 );
81 result
82 }
83 }
84 }
85
86 impl<M: ManagedTypeApi> $trait<u64> for BigUint<M> {
87 type Output = BigUint<M>;
88
89 fn $method(self, other: u64) -> BigUint<M> {
90 let big_int_temp_1 = Self::make_temp(const_handles::BIG_INT_TEMPORARY_1, other);
91 M::managed_type_impl().$api_func(
92 self.value.handle.clone(),
93 self.value.handle.clone(),
94 big_int_temp_1,
95 );
96 self
97 }
98 }
99
100 impl<'a, M: ManagedTypeApi> $trait<u64> for &'a BigUint<M> {
101 type Output = BigUint<M>;
102
103 fn $method(self, other: u64) -> BigUint<M> {
104 let big_int_temp_1 =
105 BigUint::<M>::make_temp(const_handles::BIG_INT_TEMPORARY_1, other);
106 unsafe {
107 let result = BigUint::new_uninit();
108 M::managed_type_impl().$api_func(
109 result.get_handle(),
110 self.get_handle(),
111 big_int_temp_1,
112 );
113 result
114 }
115 }
116 }
117 };
118}
119
120binary_operator! {Add, add, bi_add}
121binary_operator! {Sub, sub, bi_sub_unsigned}
122binary_operator! {Mul, mul, bi_mul}
123binary_operator! {Div, div, bi_t_div}
124binary_operator! {Rem, rem, bi_t_mod}
125binary_operator! {BitAnd, bitand, bi_and}
126binary_operator! {BitOr, bitor, bi_or}
127binary_operator! {BitXor, bitxor, bi_xor}
128
129macro_rules! binary_assign_operator {
130 ($trait:ident, $method:ident, $api_func:ident) => {
131 impl<M: ManagedTypeApi> $trait<BigUint<M>> for BigUint<M> {
132 #[inline]
133 fn $method(&mut self, other: Self) {
134 M::managed_type_impl().$api_func(
135 self.value.handle.clone(),
136 self.value.handle.clone(),
137 other.value.handle.clone(),
138 );
139 }
140 }
141
142 impl<M: ManagedTypeApi> $trait<&BigUint<M>> for BigUint<M> {
143 #[inline]
144 fn $method(&mut self, other: &BigUint<M>) {
145 M::managed_type_impl().$api_func(
146 self.value.handle.clone(),
147 self.value.handle.clone(),
148 other.value.handle.clone(),
149 );
150 }
151 }
152
153 impl<M: ManagedTypeApi> $trait<u32> for BigUint<M> {
154 fn $method(&mut self, other: u32) {
155 let big_int_temp_1 = Self::make_temp(const_handles::BIG_INT_TEMPORARY_1, other);
156 M::managed_type_impl().$api_func(
157 self.value.handle.clone(),
158 self.value.handle.clone(),
159 big_int_temp_1,
160 );
161 }
162 }
163
164 impl<M: ManagedTypeApi> $trait<u64> for BigUint<M> {
165 fn $method(&mut self, other: u64) {
166 let big_int_temp_1 = Self::make_temp(const_handles::BIG_INT_TEMPORARY_1, other);
167 M::managed_type_impl().$api_func(
168 self.value.handle.clone(),
169 self.value.handle.clone(),
170 big_int_temp_1,
171 );
172 }
173 }
174 };
175}
176
177binary_assign_operator! {AddAssign, add_assign, bi_add}
178binary_assign_operator! {SubAssign, sub_assign, bi_sub_unsigned}
179binary_assign_operator! {MulAssign, mul_assign, bi_mul}
180binary_assign_operator! {DivAssign, div_assign, bi_t_div}
181binary_assign_operator! {RemAssign, rem_assign, bi_t_mod}
182binary_assign_operator! {BitAndAssign, bitand_assign, bi_and}
183binary_assign_operator! {BitOrAssign, bitor_assign, bi_or}
184binary_assign_operator! {BitXorAssign, bitxor_assign, bi_xor}
185
186macro_rules! shift_traits {
187 ($shift_trait:ident, $method:ident, $api_func:ident) => {
188 impl<M: ManagedTypeApi> $shift_trait<usize> for BigUint<M> {
189 type Output = BigUint<M>;
190
191 #[inline]
192 fn $method(self, rhs: usize) -> BigUint<M> {
193 M::managed_type_impl().$api_func(
194 self.value.handle.clone(),
195 self.value.handle.clone(),
196 rhs,
197 );
198 self
199 }
200 }
201
202 impl<'a, M: ManagedTypeApi> $shift_trait<usize> for &'a BigUint<M> {
203 type Output = BigUint<M>;
204
205 fn $method(self, rhs: usize) -> BigUint<M> {
206 unsafe {
207 let result = BigUint::new_uninit();
208 M::managed_type_impl().$api_func(result.get_handle(), self.get_handle(), rhs);
209 result
210 }
211 }
212 }
213 };
214}
215
216shift_traits! {Shr, shr, bi_shr}
217shift_traits! {Shl, shl, bi_shl}
218
219macro_rules! shift_assign_traits {
220 ($shift_assign_trait:ident, $method:ident, $api_func:ident) => {
221 impl<M: ManagedTypeApi> $shift_assign_trait<usize> for BigUint<M> {
222 #[inline]
223 fn $method(&mut self, rhs: usize) {
224 M::managed_type_impl().$api_func(
225 self.value.handle.clone(),
226 self.value.handle.clone(),
227 rhs,
228 );
229 }
230 }
231 };
232}
233
234shift_assign_traits! {ShrAssign, shr_assign, bi_shr}
235shift_assign_traits! {ShlAssign, shl_assign, bi_shl}