Skip to content

Commit

Permalink
field: use UFCS in macro
Browse files Browse the repository at this point in the history
This is purely a code refactor to get rid of a `use` statement (which
will trigger an "unused code" warning if the macro is ever called
somewhere where the Field trait is already in scope). As a side effect
it reduces the size of the macro by roughly 75% in terms of line count.
  • Loading branch information
apoelstra committed Sep 18, 2024
1 parent 8173170 commit 8eaec9d
Showing 1 changed file with 25 additions and 84 deletions.
109 changes: 25 additions & 84 deletions src/primitives/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,222 +147,163 @@ macro_rules! impl_ops_for_fe {
impl core::ops::Add<$op> for $op {
type Output = Self;
#[inline]
fn add(self, other: $op) -> $op {
use $crate::primitives::Field as _;
self._add(&other)
}
fn add(self, other: $op) -> $op { $crate::primitives::Field::_add(&self, &other) }
}

impl core::ops::Add<&$op> for $op {
type Output = Self;
#[inline]
fn add(self, other: &$op) -> $op {
use $crate::primitives::Field as _;
self._add(other)
}
fn add(self, other: &$op) -> $op { $crate::primitives::Field::_add(&self, other) }
}

impl core::ops::Add<$op> for &$op {
type Output = $op;
#[inline]
fn add(self, other: $op) -> $op {
use $crate::primitives::Field as _;
self._add(&other)
}
fn add(self, other: $op) -> $op { $crate::primitives::Field::_add(self, &other) }
}

impl core::ops::Add<&$op> for &$op {
type Output = $op;
#[inline]
fn add(self, other: &$op) -> $op {
use $crate::primitives::Field as _;
self._add(other)
}
fn add(self, other: &$op) -> $op { $crate::primitives::Field::_add(self, other) }
}

impl core::ops::AddAssign for $op {
#[inline]
fn add_assign(&mut self, other: $op) {
use $crate::primitives::Field as _;
*self = self._add(&other)
*self = $crate::primitives::Field::_add(self, &other)
}
}

impl core::ops::AddAssign<&$op> for $op {
#[inline]
fn add_assign(&mut self, other: &$op) {
use $crate::primitives::Field as _;
*self = self._add(other)
*self = $crate::primitives::Field::_add(self, other)
}
}

// sub
impl core::ops::Sub<$op> for $op {
type Output = Self;
#[inline]
fn sub(self, other: $op) -> $op {
use $crate::primitives::Field as _;
self._sub(&other)
}
fn sub(self, other: $op) -> $op { $crate::primitives::Field::_sub(&self, &other) }
}

impl core::ops::Sub<&$op> for $op {
type Output = Self;
#[inline]
fn sub(self, other: &$op) -> $op {
use $crate::primitives::Field as _;
self._sub(other)
}
fn sub(self, other: &$op) -> $op { $crate::primitives::Field::_sub(&self, other) }
}

impl core::ops::Sub<$op> for &$op {
type Output = $op;
#[inline]
fn sub(self, other: $op) -> $op {
use $crate::primitives::Field as _;
self._sub(&other)
}
fn sub(self, other: $op) -> $op { $crate::primitives::Field::_sub(self, &other) }
}

impl core::ops::Sub<&$op> for &$op {
type Output = $op;
#[inline]
fn sub(self, other: &$op) -> $op {
use $crate::primitives::Field as _;
self._sub(other)
}
fn sub(self, other: &$op) -> $op { $crate::primitives::Field::_sub(self, other) }
}

impl core::ops::SubAssign for $op {
#[inline]
fn sub_assign(&mut self, other: $op) {
use $crate::primitives::Field as _;
*self = self._sub(&other)
*self = $crate::primitives::Field::_sub(self, &other)
}
}

impl core::ops::SubAssign<&$op> for $op {
#[inline]
fn sub_assign(&mut self, other: &$op) {
use $crate::primitives::Field as _;
*self = self._sub(other)
*self = $crate::primitives::Field::_sub(self, other)
}
}

// mul
impl core::ops::Mul<$op> for $op {
type Output = Self;
#[inline]
fn mul(self, other: $op) -> $op {
use $crate::primitives::Field as _;
self._mul(&other)
}
fn mul(self, other: $op) -> $op { $crate::primitives::Field::_mul(&self, &other) }
}

impl core::ops::Mul<&$op> for $op {
type Output = Self;
#[inline]
fn mul(self, other: &$op) -> $op {
use $crate::primitives::Field as _;
self._mul(other)
}
fn mul(self, other: &$op) -> $op { $crate::primitives::Field::_mul(&self, other) }
}

impl core::ops::Mul<$op> for &$op {
type Output = $op;
#[inline]
fn mul(self, other: $op) -> $op {
use $crate::primitives::Field as _;
self._mul(&other)
}
fn mul(self, other: $op) -> $op { $crate::primitives::Field::_mul(self, &other) }
}

impl core::ops::Mul<&$op> for &$op {
type Output = $op;
#[inline]
fn mul(self, other: &$op) -> $op {
use $crate::primitives::Field as _;
self._mul(other)
}
fn mul(self, other: &$op) -> $op { $crate::primitives::Field::_mul(self, other) }
}

impl core::ops::MulAssign for $op {
#[inline]
fn mul_assign(&mut self, other: $op) {
use $crate::primitives::Field as _;
*self = self._mul(&other)
*self = $crate::primitives::Field::_mul(self, &other)
}
}

impl core::ops::MulAssign<&$op> for $op {
#[inline]
fn mul_assign(&mut self, other: &$op) {
use $crate::primitives::Field as _;
*self = self._mul(other)
*self = $crate::primitives::Field::_mul(self, other)
}
}

// div
impl core::ops::Div<$op> for $op {
type Output = Self;
#[inline]
fn div(self, other: $op) -> $op {
use $crate::primitives::Field as _;
self._div(&other)
}
fn div(self, other: $op) -> $op { $crate::primitives::Field::_div(&self, &other) }
}

impl core::ops::Div<&$op> for $op {
type Output = Self;
#[inline]
fn div(self, other: &$op) -> $op {
use $crate::primitives::Field as _;
self._div(other)
}
fn div(self, other: &$op) -> $op { $crate::primitives::Field::_div(&self, other) }
}

impl core::ops::Div<$op> for &$op {
type Output = $op;
#[inline]
fn div(self, other: $op) -> $op {
use $crate::primitives::Field as _;
self._div(&other)
}
fn div(self, other: $op) -> $op { $crate::primitives::Field::_div(self, &other) }
}

impl core::ops::Div<&$op> for &$op {
type Output = $op;
#[inline]
fn div(self, other: &$op) -> $op {
use $crate::primitives::Field as _;
self._div(other)
}
fn div(self, other: &$op) -> $op { $crate::primitives::Field::_div(self, other) }
}

impl core::ops::DivAssign for $op {
#[inline]
fn div_assign(&mut self, other: $op) {
use $crate::primitives::Field as _;
*self = self._div(&other)
*self = $crate::primitives::Field::_div(self, &other)
}
}

impl core::ops::DivAssign<&$op> for $op {
#[inline]
fn div_assign(&mut self, other: &$op) {
use $crate::primitives::Field as _;
*self = self._div(other)
*self = $crate::primitives::Field::_div(self, other)
}
}

// neg
impl core::ops::Neg for $op {
type Output = Self;
#[inline]
fn neg(self) -> Self {
use $crate::primitives::Field as _;
self._neg()
}
fn neg(self) -> Self { $crate::primitives::Field::_neg(self) }
}

// sum
Expand Down

0 comments on commit 8eaec9d

Please sign in to comment.