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)]
54 fn $fn(self, other: Self) -> 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)]
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}