diff --git a/src/identities.rs b/src/identities.rs index 2486cb19..c30cd1dc 100644 --- a/src/identities.rs +++ b/src/identities.rs @@ -28,6 +28,13 @@ pub trait Zero: Sized + Add { fn is_zero(&self) -> bool; } +/// Defines an associated constant representing the additive identity element +/// for `Self`. +pub trait ConstZero: Zero { + /// The additive identity element of `Self`, `0`. + const ZERO: Self; +} + macro_rules! zero_impl { ($t:ty, $v:expr) => { impl Zero for $t { @@ -40,6 +47,10 @@ macro_rules! zero_impl { *self == $v } } + + impl ConstZero for $t { + const ZERO: Self = $v; + } }; } @@ -77,6 +88,13 @@ where } } +impl ConstZero for Wrapping +where + Wrapping: Add>, +{ + const ZERO: Self = Wrapping(T::ZERO); +} + /// Defines a multiplicative identity element for `Self`. /// /// # Laws @@ -115,6 +133,13 @@ pub trait One: Sized + Mul { } } +/// Defines an associated constant representing the multiplicative identity +/// element for `Self`. +pub trait ConstOne: One { + /// The multiplicative identity element of `Self`, `1`. + const ONE: Self; +} + macro_rules! one_impl { ($t:ty, $v:expr) => { impl One for $t { @@ -127,6 +152,10 @@ macro_rules! one_impl { *self == $v } } + + impl ConstOne for $t { + const ONE: Self = $v; + } }; } @@ -160,6 +189,13 @@ where } } +impl ConstOne for Wrapping +where + Wrapping: Mul>, +{ + const ONE: Self = Wrapping(T::ONE); +} + // Some helper functions provided for backwards compatibility. /// Returns the additive identity, `0`. diff --git a/src/lib.rs b/src/lib.rs index 54dab6e5..9ee16fc8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,7 +33,7 @@ pub use crate::float::Float; pub use crate::float::FloatConst; // pub use real::{FloatCore, Real}; // NOTE: Don't do this, it breaks `use num_traits::*;`. pub use crate::cast::{cast, AsPrimitive, FromPrimitive, NumCast, ToPrimitive}; -pub use crate::identities::{one, zero, One, Zero}; +pub use crate::identities::{one, zero, ConstOne, ConstZero, One, Zero}; pub use crate::int::PrimInt; pub use crate::ops::bytes::{FromBytes, ToBytes}; pub use crate::ops::checked::{