diff --git a/traits/src/identities.rs b/traits/src/identities.rs index 15f0c90..889b11d 100644 --- a/traits/src/identities.rs +++ b/traits/src/identities.rs @@ -1,4 +1,5 @@ use std::ops::{Add, Mul}; +use std::num::Wrapping; /// Defines an additive identity element for `Self`. pub trait Zero: Sized + Add { @@ -50,6 +51,16 @@ zero_impl!(i64, 0i64); zero_impl!(f32, 0.0f32); zero_impl!(f64, 0.0f64); +impl Zero for Wrapping where Wrapping: Add> { + fn is_zero(&self) -> bool { + self.0.is_zero() + } + fn zero() -> Self { + Wrapping(T::zero()) + } +} + + /// Defines a multiplicative identity element for `Self`. pub trait One: Sized + Mul { /// Returns the multiplicative identity element of `Self`, `1`. @@ -94,6 +105,11 @@ one_impl!(i64, 1i64); one_impl!(f32, 1.0f32); one_impl!(f64, 1.0f64); +impl One for Wrapping where Wrapping: Mul> { + fn one() -> Self { + Wrapping(T::one()) + } +} // Some helper functions provided for backwards compatibility. diff --git a/traits/src/lib.rs b/traits/src/lib.rs index 4b4ed9c..0cf7e34 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -15,6 +15,7 @@ html_playground_url = "http://play.integer32.com/")] use std::ops::{Add, Sub, Mul, Div, Rem}; +use std::num::Wrapping; pub use bounds::Bounded; pub use float::{Float, FloatConst}; @@ -74,6 +75,18 @@ macro_rules! int_trait_impl { } int_trait_impl!(Num for usize u8 u16 u32 u64 isize i8 i16 i32 i64); +impl Num for Wrapping + where Wrapping: + Add> + Sub> + + Mul> + Div> + Rem> +{ + type FromStrRadixErr = T::FromStrRadixErr; + fn from_str_radix(str: &str, radix: u32) -> Result { + T::from_str_radix(str, radix).map(Wrapping) + } +} + + #[derive(Debug)] pub enum FloatErrorKind { Empty, @@ -243,9 +256,9 @@ float_trait_impl!(Num for f32 f64); /// A value bounded by a minimum and a maximum /// -/// If input is less than min then this returns min. -/// If input is greater than max then this returns max. -/// Otherwise this returns input. +/// If input is less than min then this returns min. +/// If input is greater than max then this returns max. +/// Otherwise this returns input. #[inline] pub fn clamp(input: T, min: T, max: T) -> T { debug_assert!(min <= max, "min must be less than or equal to max");