Skip to content

Commit

Permalink
Remove blanket impls; add impls for Wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri authored and cuviper committed Feb 8, 2024
1 parent 06c0ee0 commit 536dcf2
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions src/identities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,19 @@ pub trait ZeroConstant: Zero {
const ZERO: Self;
}

impl<T: ZeroConstant + PartialEq> Zero for T {
#[inline(always)]
fn zero() -> T {
Self::ZERO
}

#[inline(always)]
fn is_zero(&self) -> bool {
self == &Self::ZERO
}
}

macro_rules! zero_impl {
($t:ty, $v:expr) => {
impl Zero for $t {
#[inline]
fn zero() -> $t {
$v
}
#[inline]
fn is_zero(&self) -> bool {
*self == $v
}
}

impl ZeroConstant for $t {
const ZERO: Self = $v;
}
Expand Down Expand Up @@ -92,6 +91,13 @@ where
}
}

impl<T: ZeroConstant> ZeroConstant for Wrapping<T>
where
Wrapping<T>: Add<Output = Wrapping<T>>,
{
const ZERO: Self = Self(T::ZERO);
}

/// Defines a multiplicative identity element for `Self`.
///
/// # Laws
Expand Down Expand Up @@ -140,20 +146,19 @@ pub trait OneConstant: One {
const ONE: Self;
}

impl<T: OneConstant + PartialEq> One for T {
#[inline(always)]
fn one() -> T {
Self::ONE
}

#[inline(always)]
fn is_one(&self) -> bool {
self == &Self::ONE
}
}

macro_rules! one_impl {
($t:ty, $v:expr) => {
impl One for $t {
#[inline]
fn one() -> $t {
$v
}
#[inline]
fn is_one(&self) -> bool {
*self == $v
}
}

impl OneConstant for $t {
const ONE: Self = $v;
}
Expand Down Expand Up @@ -190,6 +195,13 @@ where
}
}

impl<T: OneConstant> OneConstant for Wrapping<T>
where
Wrapping<T>: Mul<Output = Wrapping<T>>,
{
const ONE: Self = Self(T::ONE);
}

// Some helper functions provided for backwards compatibility.

/// Returns the additive identity, `0`.
Expand Down

0 comments on commit 536dcf2

Please sign in to comment.