arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
ark-r1cs-std/src/macros.rs
src/macros.rsRaw
  1#[allow(unused_braces)]
  2/// Implements arithmetic traits (eg: `Add`, `Sub`, `Mul`) for the given type
  3/// using the impl in `$impl`.
  4///
  5/// Used primarily for implementing these traits for `FieldVar`s and
  6/// `GroupVar`s.
  7#[macro_export]
  8macro_rules! impl_ops {
  9    (
 10        $type: ty,
 11        $native: ty,
 12        $trait: ident,
 13        $fn: ident,
 14        $assign_trait: ident,
 15        $assign_fn: ident,
 16        $impl: expr,
 17        $constant_impl: expr,
 18        $($args:tt)*
 19    ) => {
 20        impl_bounded_ops!($type, $native, $trait, $fn, $assign_trait, $assign_fn, $impl, $constant_impl, ($($args)+), );
 21    };
 22}
 23
 24/// Implements arithmetic traits (eg: `Add`, `Sub`, `Mul`) for the given type
 25/// using the impl in `$impl`.
 26///
 27/// Used primarily for implementing these traits for `FieldVar`s and
 28/// `GroupVar`s.
 29///
 30/// When compared to `impl_ops`, this macro allows specifying additional trait
 31/// bounds.
 32#[macro_export]
 33macro_rules! impl_bounded_ops {
 34    (
 35        $type: ty,
 36        $native: ty,
 37        $trait: ident,
 38        $fn: ident,
 39        $assign_trait: ident,
 40        $assign_fn: ident,
 41        $impl: expr,
 42        $constant_impl: expr,
 43        ($($params:tt)+),
 44        $($bounds:tt)*
 45    ) => {
 46        impl<'a, $($params)+> core::ops::$trait<&'a $type> for &'a $type
 47        where
 48            $($bounds)*
 49        {
 50            type Output = $type;
 51
 52            #[tracing::instrument(target = "r1cs", skip(self))]
 53            #[allow(unused_braces, clippy::redundant_closure_call)]
 54            fn $fn(self, other: &'a $type) -> Self::Output {
 55                ($impl)(self, other)
 56            }
 57        }
 58
 59        impl<'a, $($params)+> core::ops::$trait<$type> for &'a $type
 60        where
 61            $($bounds)*
 62        {
 63            type Output = $type;
 64
 65            #[tracing::instrument(target = "r1cs", skip(self))]
 66            #[allow(unused_braces)]
 67            fn $fn(self, other: $type) -> Self::Output {
 68                core::ops::$trait::$fn(self, &other)
 69            }
 70        }
 71
 72        impl<'a, $($params)+> core::ops::$trait<&'a $type> for $type
 73        where
 74            $($bounds)*
 75        {
 76            type Output = $type;
 77
 78            #[tracing::instrument(target = "r1cs", skip(self))]
 79            #[allow(unused_braces)]
 80            fn $fn(self, other: &'a $type) -> Self::Output {
 81                core::ops::$trait::$fn(&self, other)
 82            }
 83        }
 84
 85        impl<$($params)+> core::ops::$trait<$type> for $type
 86        where
 87
 88            $($bounds)*
 89        {
 90            type Output = $type;
 91
 92            #[tracing::instrument(target = "r1cs", skip(self))]
 93            #[allow(unused_braces)]
 94            fn $fn(self, other: $type) -> Self::Output {
 95                core::ops::$trait::$fn(&self, &other)
 96            }
 97        }
 98
 99        impl<$($params)+> core::ops::$assign_trait<$type> for $type
100        where
101
102            $($bounds)*
103        {
104            #[tracing::instrument(target = "r1cs", skip(self))]
105            #[allow(unused_braces)]
106            fn $assign_fn(&mut self, other: $type) {
107                let result = core::ops::$trait::$fn(&*self, &other);
108                *self = result
109            }
110        }
111
112        impl<'a, $($params)+> core::ops::$assign_trait<&'a $type> for $type
113        where
114
115            $($bounds)*
116        {
117            #[tracing::instrument(target = "r1cs", skip(self))]
118            #[allow(unused_braces)]
119            fn $assign_fn(&mut self, other: &'a $type) {
120                let result = core::ops::$trait::$fn(&*self, other);
121                *self = result
122            }
123        }
124
125        impl<'a, $($params)+> core::ops::$trait<$native> for &'a $type
126        where
127
128            $($bounds)*
129        {
130            type Output = $type;
131
132            #[tracing::instrument(target = "r1cs", skip(self))]
133            #[allow(unused_braces, clippy::redundant_closure_call)]
134            fn $fn(self, other: $native) -> Self::Output {
135                ($constant_impl)(self, other)
136            }
137        }
138
139        impl<$($params)+> core::ops::$trait<$native> for $type
140        where
141
142            $($bounds)*
143        {
144            type Output = $type;
145
146            #[tracing::instrument(target = "r1cs", skip(self))]
147            #[allow(unused_braces)]
148            fn $fn(self, other: $native) -> Self::Output {
149                core::ops::$trait::$fn(&self, other)
150            }
151        }
152
153        impl<$($params)+> core::ops::$assign_trait<$native> for $type
154        where
155
156            $($bounds)*
157        {
158
159            #[tracing::instrument(target = "r1cs", skip(self))]
160            #[allow(unused_braces)]
161            fn $assign_fn(&mut self, other: $native) {
162                let result = core::ops::$trait::$fn(&*self, other);
163                *self = result
164            }
165        }
166    }
167}
168
169/// Implements arithmetic traits (eg: `Add`, `Sub`, `Mul`) for the given type
170/// using the impl in `$impl`.
171///
172/// Used primarily for implementing these traits for `FieldVar`s and
173/// `GroupVar`s.
174///
175/// When compared to `impl_ops`, this macro allows specifying additional trait
176/// bounds.
177#[macro_export]
178macro_rules! impl_bounded_ops_diff {
179    (
180        $type: ty,
181        $native: ty,
182        $other_type: ty,
183        $other_native: ty,
184        $trait: ident,
185        $fn: ident,
186        $assign_trait: ident,
187        $assign_fn: ident,
188        $impl: expr,
189        $constant_impl: expr,
190        ($($params:tt)+),
191        $($bounds:tt)*
192    ) => {
193        impl<'a, $($params)+> core::ops::$trait<&'a $other_type> for &'a $type
194        where
195            $($bounds)*
196        {
197            type Output = $type;
198
199            #[tracing::instrument(target = "r1cs", skip(self))]
200            #[allow(unused_braces, clippy::redundant_closure_call)]
201            fn $fn(self, other: &'a $other_type) -> Self::Output {
202                ($impl)(self, other)
203            }
204        }
205
206        impl<'a, $($params)+> core::ops::$trait<$other_type> for &'a $type
207        where
208            $($bounds)*
209        {
210            type Output = $type;
211
212            #[tracing::instrument(target = "r1cs", skip(self))]
213            #[allow(unused_braces)]
214            fn $fn(self, other: $other_type) -> Self::Output {
215                core::ops::$trait::$fn(self, &other)
216            }
217        }
218
219        impl<'a, $($params)+> core::ops::$trait<&'a $other_type> for $type
220        where
221            $($bounds)*
222        {
223            type Output = $type;
224
225            #[tracing::instrument(target = "r1cs", skip(self))]
226            #[allow(unused_braces)]
227            fn $fn(self, other: &'a $other_type) -> Self::Output {
228                core::ops::$trait::$fn(&self, other)
229            }
230        }
231
232        impl<$($params)+> core::ops::$trait<$other_type> for $type
233        where
234
235            $($bounds)*
236        {
237            type Output = $type;
238
239            #[tracing::instrument(target = "r1cs", skip(self))]
240            #[allow(unused_braces)]
241            fn $fn(self, other: $other_type) -> Self::Output {
242                core::ops::$trait::$fn(&self, &other)
243            }
244        }
245
246        impl<$($params)+> core::ops::$assign_trait<$other_type> for $type
247        where
248
249            $($bounds)*
250        {
251            #[tracing::instrument(target = "r1cs", skip(self))]
252            #[allow(unused_braces)]
253            fn $assign_fn(&mut self, other: $other_type) {
254                let result = core::ops::$trait::$fn(&*self, &other);
255                *self = result
256            }
257        }
258
259        impl<'a, $($params)+> core::ops::$assign_trait<&'a $other_type> for $type
260        where
261
262            $($bounds)*
263        {
264            #[tracing::instrument(target = "r1cs", skip(self))]
265            #[allow(unused_braces)]
266            fn $assign_fn(&mut self, other: &'a $other_type) {
267                let result = core::ops::$trait::$fn(&*self, other);
268                *self = result
269            }
270        }
271    }
272}