arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
ark-r1cs-std/r1cs-std/src/macros.rs
r1cs-std/src/macros.rsRaw
  1#[allow(unused_braces)]
  2// Implements arithmetic operations with generic bounds.
  3#[macro_export]
  4macro_rules! impl_ops {
  5    (
  6        $type: ty,
  7        $native: ty,
  8        $trait: ident,
  9        $fn: ident,
 10        $assign_trait: ident,
 11        $assign_fn: ident,
 12        $impl: expr,
 13        $constant_impl: expr,
 14        $($args:tt)*
 15    ) => {
 16        impl_bounded_ops!($type, $native, $trait, $fn, $assign_trait, $assign_fn, $impl, $constant_impl, ($($args)+), );
 17    };
 18}
 19
 20#[macro_export]
 21macro_rules! impl_bounded_ops {
 22    (
 23        $type: ty,
 24        $native: ty,
 25        $trait: ident,
 26        $fn: ident,
 27        $assign_trait: ident,
 28        $assign_fn: ident,
 29        $impl: expr,
 30        $constant_impl: expr,
 31        ($($params:tt)+),
 32        $($bounds:tt)*
 33    ) => {
 34        impl<'a, $($params)+> core::ops::$trait<&'a $type> for &'a $type
 35        where
 36            $($bounds)*
 37        {
 38            type Output = $type;
 39
 40            #[tracing::instrument(target = "r1cs", skip(self))]
 41            #[allow(unused_braces)]
 42            fn $fn(self, other: Self) -> Self::Output {
 43                $impl(self, other)
 44            }
 45        }
 46
 47        impl<'a, $($params)+> core::ops::$trait<$type> for &'a $type
 48        where
 49            $($bounds)*
 50        {
 51            type Output = $type;
 52
 53            #[tracing::instrument(target = "r1cs", skip(self))]
 54            #[allow(unused_braces)]
 55            fn $fn(self, other: $type) -> Self::Output {
 56                core::ops::$trait::$fn(self, &other)
 57            }
 58        }
 59
 60        impl<'a, $($params)+> core::ops::$trait<&'a $type> for $type
 61        where
 62            $($bounds)*
 63        {
 64            type Output = $type;
 65
 66            #[tracing::instrument(target = "r1cs", skip(self))]
 67            #[allow(unused_braces)]
 68            fn $fn(self, other: &'a $type) -> Self::Output {
 69                core::ops::$trait::$fn(&self, other)
 70            }
 71        }
 72
 73        impl<$($params)+> core::ops::$trait<$type> for $type
 74        where
 75
 76            $($bounds)*
 77        {
 78            type Output = $type;
 79
 80            #[tracing::instrument(target = "r1cs", skip(self))]
 81            #[allow(unused_braces)]
 82            fn $fn(self, other: $type) -> Self::Output {
 83                core::ops::$trait::$fn(&self, &other)
 84            }
 85        }
 86
 87        impl<$($params)+> core::ops::$assign_trait<$type> for $type
 88        where
 89
 90            $($bounds)*
 91        {
 92            #[tracing::instrument(target = "r1cs", skip(self))]
 93            #[allow(unused_braces)]
 94            fn $assign_fn(&mut self, other: $type) {
 95                let result = core::ops::$trait::$fn(&*self, &other);
 96                *self = result
 97            }
 98        }
 99
100        impl<'a, $($params)+> core::ops::$assign_trait<&'a $type> for $type
101        where
102
103            $($bounds)*
104        {
105            #[tracing::instrument(target = "r1cs", skip(self))]
106            #[allow(unused_braces)]
107            fn $assign_fn(&mut self, other: &'a $type) {
108                let result = core::ops::$trait::$fn(&*self, other);
109                *self = result
110            }
111        }
112
113        impl<'a, $($params)+> core::ops::$trait<$native> for &'a $type
114        where
115
116            $($bounds)*
117        {
118            type Output = $type;
119
120            #[tracing::instrument(target = "r1cs", skip(self))]
121            #[allow(unused_braces)]
122            fn $fn(self, other: $native) -> Self::Output {
123                $constant_impl(self, other)
124            }
125        }
126
127        impl<$($params)+> core::ops::$trait<$native> for $type
128        where
129
130            $($bounds)*
131        {
132            type Output = $type;
133
134            #[tracing::instrument(target = "r1cs", skip(self))]
135            #[allow(unused_braces)]
136            fn $fn(self, other: $native) -> Self::Output {
137                core::ops::$trait::$fn(&self, other)
138            }
139        }
140
141        impl<$($params)+> core::ops::$assign_trait<$native> for $type
142        where
143
144            $($bounds)*
145        {
146
147            #[tracing::instrument(target = "r1cs", skip(self))]
148            #[allow(unused_braces)]
149            fn $assign_fn(&mut self, other: $native) {
150                let result = core::ops::$trait::$fn(&*self, other);
151                *self = result
152            }
153        }
154    }
155}