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