From b024e1c3269226fafe0cf35a2d3ebc11862b9043 Mon Sep 17 00:00:00 2001 From: Yoan Lecoq Date: Fri, 21 Apr 2017 09:26:53 +0200 Subject: [PATCH 1/5] Implemented Zero, One and Num for Wrapping --- traits/src/identities.rs | 16 ++++++++++++++++ traits/src/lib.rs | 19 ++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/traits/src/identities.rs b/traits/src/identities.rs index 15f0c90..64d9e72 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 Self: Add { + fn is_zero(&self) -> bool { + self.0 == T::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 Self: Add + 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..21b3953 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 Self: Zero + One + + 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(|x| Wrapping(x)) + } +} + + #[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"); From 9b06d4a0bbe8a7346286444707fcaa477a384f6d Mon Sep 17 00:00:00 2001 From: Yoan Lecoq Date: Fri, 21 Apr 2017 10:31:29 +0200 Subject: [PATCH 2/5] Attempt at fixing E0411 on older rust versions --- traits/src/identities.rs | 4 ++-- traits/src/lib.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/traits/src/identities.rs b/traits/src/identities.rs index 64d9e72..bc6c084 100644 --- a/traits/src/identities.rs +++ b/traits/src/identities.rs @@ -51,7 +51,7 @@ zero_impl!(i64, 0i64); zero_impl!(f32, 0.0f32); zero_impl!(f64, 0.0f64); -impl Zero for Wrapping where Self: Add { +impl Zero for Wrapping where Wrapping: Add> { fn is_zero(&self) -> bool { self.0 == T::zero() } @@ -105,7 +105,7 @@ one_impl!(i64, 1i64); one_impl!(f32, 1.0f32); one_impl!(f64, 1.0f64); -impl One for Wrapping where Self: Add + Mul { +impl One for Wrapping where Wrapping: Add> + Mul> { fn one() -> Self { Wrapping(T::one()) } diff --git a/traits/src/lib.rs b/traits/src/lib.rs index 21b3953..16d8fde 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -76,9 +76,9 @@ 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 Self: Zero + One - + Add + Sub - + Mul + Div + Rem + where Wrapping: Zero + One + + Add> + Sub> + + Mul> + Div> + Rem> { type FromStrRadixErr = T::FromStrRadixErr; fn from_str_radix(str: &str, radix: u32) -> Result { From b562c1ec39c3cd332804ed81ec6d4bdeb87599db Mon Sep 17 00:00:00 2001 From: Yoan Lecoq Date: Fri, 21 Apr 2017 18:47:42 +0200 Subject: [PATCH 3/5] Minor changes for correctness --- traits/src/identities.rs | 2 +- traits/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/traits/src/identities.rs b/traits/src/identities.rs index bc6c084..7ce4af5 100644 --- a/traits/src/identities.rs +++ b/traits/src/identities.rs @@ -53,7 +53,7 @@ zero_impl!(f64, 0.0f64); impl Zero for Wrapping where Wrapping: Add> { fn is_zero(&self) -> bool { - self.0 == T::zero() + self.0.is_zero() } fn zero() -> Self { Wrapping(T::zero()) diff --git a/traits/src/lib.rs b/traits/src/lib.rs index 16d8fde..b61edad 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -82,7 +82,7 @@ impl Num for Wrapping { type FromStrRadixErr = T::FromStrRadixErr; fn from_str_radix(str: &str, radix: u32) -> Result { - T::from_str_radix(str, radix).map(|x| Wrapping(x)) + T::from_str_radix(str, radix).map(Wrapping) } } From 356a4ba5b5de1e5fba4bfa8ecc7adff7505f54e8 Mon Sep 17 00:00:00 2001 From: Yoan Lecoq Date: Fri, 21 Apr 2017 18:56:23 +0200 Subject: [PATCH 4/5] Removed useless trait bounds --- traits/src/identities.rs | 2 +- traits/src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/traits/src/identities.rs b/traits/src/identities.rs index 7ce4af5..6bd9f00 100644 --- a/traits/src/identities.rs +++ b/traits/src/identities.rs @@ -51,7 +51,7 @@ zero_impl!(i64, 0i64); zero_impl!(f32, 0.0f32); zero_impl!(f64, 0.0f64); -impl Zero for Wrapping where Wrapping: Add> { +impl Zero for Wrapping where Wrapping: Add> { fn is_zero(&self) -> bool { self.0.is_zero() } diff --git a/traits/src/lib.rs b/traits/src/lib.rs index b61edad..0cf7e34 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -76,8 +76,8 @@ 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: Zero + One - + Add> + Sub> + where Wrapping: + Add> + Sub> + Mul> + Div> + Rem> { type FromStrRadixErr = T::FromStrRadixErr; From 2172a9368bca7580dc3260658da05770939dbd7a Mon Sep 17 00:00:00 2001 From: Yoan Lecoq Date: Fri, 21 Apr 2017 19:10:42 +0200 Subject: [PATCH 5/5] impl One for Wrapping doesn't require Add --- traits/src/identities.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/traits/src/identities.rs b/traits/src/identities.rs index 6bd9f00..889b11d 100644 --- a/traits/src/identities.rs +++ b/traits/src/identities.rs @@ -105,7 +105,7 @@ one_impl!(i64, 1i64); one_impl!(f32, 1.0f32); one_impl!(f64, 1.0f64); -impl One for Wrapping where Wrapping: Add> + Mul> { +impl One for Wrapping where Wrapping: Mul> { fn one() -> Self { Wrapping(T::one()) }