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
19macro_rules! impl_bounded_ops {
20 (
21 $type: ty,
22 $native: ty,
23 $trait: ident,
24 $fn: ident,
25 $assign_trait: ident,
26 $assign_fn: ident,
27 $impl: expr,
28 $constant_impl: expr,
29 ($($params:tt)+),
30 $($bounds:tt)*
31 ) => {
32 impl<'a, $($params)+> core::ops::$trait<&'a $type> for &'a $type
33 where
34 $($bounds)*
35 {
36 type Output = $type;
37
38 fn $fn(self, other: Self) -> Self::Output {
39 $impl(self, other)
40 }
41 }
42
43 impl<'a, $($params)+> core::ops::$trait<$type> for &'a $type
44 where
45 $($bounds)*
46 {
47 type Output = $type;
48
49 fn $fn(self, other: $type) -> Self::Output {
50 core::ops::$trait::$fn(self, &other)
51 }
52 }
53
54 impl<'a, $($params)+> core::ops::$trait<&'a $type> for $type
55 where
56 $($bounds)*
57 {
58 type Output = $type;
59
60 fn $fn(self, other: &'a $type) -> Self::Output {
61 core::ops::$trait::$fn(&self, other)
62 }
63 }
64
65 impl<$($params)+> core::ops::$trait<$type> for $type
66 where
67
68 $($bounds)*
69 {
70 type Output = $type;
71
72 fn $fn(self, other: $type) -> Self::Output {
73 core::ops::$trait::$fn(&self, &other)
74 }
75 }
76
77 impl<$($params)+> core::ops::$assign_trait<$type> for $type
78 where
79
80 $($bounds)*
81 {
82 fn $assign_fn(&mut self, other: $type) {
83 let result = core::ops::$trait::$fn(&*self, &other);
84 *self = result
85 }
86 }
87
88 impl<'a, $($params)+> core::ops::$assign_trait<&'a $type> for $type
89 where
90
91 $($bounds)*
92 {
93 fn $assign_fn(&mut self, other: &'a $type) {
94 let result = core::ops::$trait::$fn(&*self, other);
95 *self = result
96 }
97 }
98
99 impl<'a, $($params)+> core::ops::$trait<$native> for &'a $type
100 where
101
102 $($bounds)*
103 {
104 type Output = $type;
105
106 fn $fn(self, other: $native) -> Self::Output {
107 $constant_impl(self, other)
108 }
109 }
110
111 impl<$($params)+> core::ops::$trait<$native> for $type
112 where
113
114 $($bounds)*
115 {
116 type Output = $type;
117
118 fn $fn(self, other: $native) -> Self::Output {
119 core::ops::$trait::$fn(&self, other)
120 }
121 }
122
123 impl<$($params)+> core::ops::$assign_trait<$native> for $type
124 where
125
126 $($bounds)*
127 {
128
129 fn $assign_fn(&mut self, other: $native) {
130 let result = core::ops::$trait::$fn(&*self, other);
131 *self = result
132 }
133 }
134 }
135}