You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.7 KiB

  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]
  8. macro_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. /// Implements arithmetic traits (eg: `Add`, `Sub`, `Mul`) for the given type
  24. /// using the impl in `$impl`.
  25. ///
  26. /// Used primarily for implementing these traits for `FieldVar`s and
  27. /// `GroupVar`s.
  28. ///
  29. /// When compared to `impl_ops`, this macro allows specifying additional trait
  30. /// bounds.
  31. #[macro_export]
  32. macro_rules! impl_bounded_ops {
  33. (
  34. $type: ty,
  35. $native: ty,
  36. $trait: ident,
  37. $fn: ident,
  38. $assign_trait: ident,
  39. $assign_fn: ident,
  40. $impl: expr,
  41. $constant_impl: expr,
  42. ($($params:tt)+),
  43. $($bounds:tt)*
  44. ) => {
  45. impl<'a, $($params)+> core::ops::$trait<&'a $type> for &'a $type
  46. where
  47. $($bounds)*
  48. {
  49. type Output = $type;
  50. #[tracing::instrument(target = "r1cs", skip(self))]
  51. #[allow(unused_braces, clippy::redundant_closure_call)]
  52. fn $fn(self, other: Self) -> Self::Output {
  53. ($impl)(self, other)
  54. }
  55. }
  56. impl<'a, $($params)+> core::ops::$trait<$type> for &'a $type
  57. where
  58. $($bounds)*
  59. {
  60. type Output = $type;
  61. #[tracing::instrument(target = "r1cs", skip(self))]
  62. #[allow(unused_braces)]
  63. fn $fn(self, other: $type) -> Self::Output {
  64. core::ops::$trait::$fn(self, &other)
  65. }
  66. }
  67. impl<'a, $($params)+> core::ops::$trait<&'a $type> for $type
  68. where
  69. $($bounds)*
  70. {
  71. type Output = $type;
  72. #[tracing::instrument(target = "r1cs", skip(self))]
  73. #[allow(unused_braces)]
  74. fn $fn(self, other: &'a $type) -> Self::Output {
  75. core::ops::$trait::$fn(&self, other)
  76. }
  77. }
  78. impl<$($params)+> core::ops::$trait<$type> for $type
  79. where
  80. $($bounds)*
  81. {
  82. type Output = $type;
  83. #[tracing::instrument(target = "r1cs", skip(self))]
  84. #[allow(unused_braces)]
  85. fn $fn(self, other: $type) -> Self::Output {
  86. core::ops::$trait::$fn(&self, &other)
  87. }
  88. }
  89. impl<$($params)+> core::ops::$assign_trait<$type> for $type
  90. where
  91. $($bounds)*
  92. {
  93. #[tracing::instrument(target = "r1cs", skip(self))]
  94. #[allow(unused_braces)]
  95. fn $assign_fn(&mut self, other: $type) {
  96. let result = core::ops::$trait::$fn(&*self, &other);
  97. *self = result
  98. }
  99. }
  100. impl<'a, $($params)+> core::ops::$assign_trait<&'a $type> for $type
  101. where
  102. $($bounds)*
  103. {
  104. #[tracing::instrument(target = "r1cs", skip(self))]
  105. #[allow(unused_braces)]
  106. fn $assign_fn(&mut self, other: &'a $type) {
  107. let result = core::ops::$trait::$fn(&*self, other);
  108. *self = result
  109. }
  110. }
  111. impl<'a, $($params)+> core::ops::$trait<$native> for &'a $type
  112. where
  113. $($bounds)*
  114. {
  115. type Output = $type;
  116. #[tracing::instrument(target = "r1cs", skip(self))]
  117. #[allow(unused_braces, clippy::redundant_closure_call)]
  118. fn $fn(self, other: $native) -> Self::Output {
  119. ($constant_impl)(self, other)
  120. }
  121. }
  122. impl<$($params)+> core::ops::$trait<$native> for $type
  123. where
  124. $($bounds)*
  125. {
  126. type Output = $type;
  127. #[tracing::instrument(target = "r1cs", skip(self))]
  128. #[allow(unused_braces)]
  129. fn $fn(self, other: $native) -> Self::Output {
  130. core::ops::$trait::$fn(&self, other)
  131. }
  132. }
  133. impl<$($params)+> core::ops::$assign_trait<$native> for $type
  134. where
  135. $($bounds)*
  136. {
  137. #[tracing::instrument(target = "r1cs", skip(self))]
  138. #[allow(unused_braces)]
  139. fn $assign_fn(&mut self, other: $native) {
  140. let result = core::ops::$trait::$fn(&*self, other);
  141. *self = result
  142. }
  143. }
  144. }
  145. }