From 8634d2fad620c2b50e929eb9c66a36f09cd528e2 Mon Sep 17 00:00:00 2001 From: Fabio Krapohl Date: Wed, 17 Jan 2018 13:50:33 +0100 Subject: [PATCH 1/3] associated consts --- Cargo.toml | 2 +- src/identities.rs | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c388e9e..8870830 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ categories = [ "algorithms", "science" ] license = "MIT/Apache-2.0" repository = "https://github.com/rust-num/num-traits" name = "num-traits" -version = "0.1.41" +version = "0.2.0" readme = "README.md" [dependencies] diff --git a/src/identities.rs b/src/identities.rs index 79882ed..99c7e46 100644 --- a/src/identities.rs +++ b/src/identities.rs @@ -18,7 +18,11 @@ pub trait Zero: Sized + Add { /// external mutable state, for example values stored in TLS or in /// `static mut`s. // FIXME (#5527): This should be an associated constant - fn zero() -> Self; + const ZERO: Self; + + fn zero() -> Self { + Self::ZERO + } /// Returns `true` if `self` is equal to the additive identity. #[inline] @@ -28,6 +32,7 @@ pub trait Zero: Sized + Add { macro_rules! zero_impl { ($t:ty, $v:expr) => { impl Zero for $t { + const ZERO: $t = $v; #[inline] fn zero() -> $t { $v } #[inline] @@ -52,12 +57,12 @@ 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()) - } + const ZERO: Self = Wrapping(T::ZERO); } @@ -78,12 +83,17 @@ pub trait One: Sized + Mul { /// external mutable state, for example values stored in TLS or in /// `static mut`s. // FIXME (#5527): This should be an associated constant - fn one() -> Self; + const ONE: Self; + + fn one() -> Self { + Self::ONE + } } macro_rules! one_impl { ($t:ty, $v:expr) => { impl One for $t { + const ONE: $t = $v; #[inline] fn one() -> $t { $v } } @@ -106,9 +116,7 @@ one_impl!(f32, 1.0f32); one_impl!(f64, 1.0f64); impl One for Wrapping where Wrapping: Mul> { - fn one() -> Self { - Wrapping(T::one()) - } + const ONE: Self = Wrapping(T::ONE); } // Some helper functions provided for backwards compatibility. From 9d3db0e9ea11b45e6ed35f3f2640811016abbc2e Mon Sep 17 00:00:00 2001 From: Fabio Krapohl Date: Wed, 17 Jan 2018 14:10:59 +0100 Subject: [PATCH 2/3] fix all fixme #5527 --- src/bounds.rs | 35 +++++++++++++++++------------------ src/identities.rs | 2 ++ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/bounds.rs b/src/bounds.rs index 83fdd0f..84cb670 100644 --- a/src/bounds.rs +++ b/src/bounds.rs @@ -5,21 +5,25 @@ use std::num::Wrapping; /// Numbers which have upper and lower bounds pub trait Bounded { - // FIXME (#5527): These should be associated constants /// returns the smallest finite number this type can represent - fn min_value() -> Self; + const MIN: Self; + #[inline] + fn min_value() -> Self where Self: Sized { + Self::MIN + } /// returns the largest finite number this type can represent - fn max_value() -> Self; + const MAX: Self; + #[inline] + fn max_value() -> Self where Self: Sized { + Self::MAX + } } macro_rules! bounded_impl { ($t:ty, $min:expr, $max:expr) => { impl Bounded for $t { - #[inline] - fn min_value() -> $t { $min } - - #[inline] - fn max_value() -> $t { $max } + const MIN: $t = $min; + const MAX: $t = $max; } } } @@ -37,8 +41,9 @@ bounded_impl!(i32, i32::MIN, i32::MAX); bounded_impl!(i64, i64::MIN, i64::MAX); impl Bounded for Wrapping { - fn min_value() -> Self { Wrapping(T::min_value()) } - fn max_value() -> Self { Wrapping(T::max_value()) } + + const MIN: Self = Wrapping(T::MIN); + const MAX: Self = Wrapping(T::MAX); } bounded_impl!(f32, f32::MIN, f32::MAX); @@ -61,14 +66,8 @@ macro_rules! for_each_tuple { macro_rules! bounded_tuple { ( $($name:ident)* ) => ( impl<$($name: Bounded,)*> Bounded for ($($name,)*) { - #[inline] - fn min_value() -> Self { - ($($name::min_value(),)*) - } - #[inline] - fn max_value() -> Self { - ($($name::max_value(),)*) - } + const MIN: Self = ($($name::MIN,)*); + const MAX: Self = ($($name::MAX,)*); } ); } diff --git a/src/identities.rs b/src/identities.rs index 99c7e46..0360a19 100644 --- a/src/identities.rs +++ b/src/identities.rs @@ -20,6 +20,7 @@ pub trait Zero: Sized + Add { // FIXME (#5527): This should be an associated constant const ZERO: Self; + #[inline] fn zero() -> Self { Self::ZERO } @@ -85,6 +86,7 @@ pub trait One: Sized + Mul { // FIXME (#5527): This should be an associated constant const ONE: Self; + #[inline] fn one() -> Self { Self::ONE } From c58ae9cfadb409782a99e56149211292fd0a6b3d Mon Sep 17 00:00:00 2001 From: Fabio Krapohl Date: Wed, 17 Jan 2018 14:12:00 +0100 Subject: [PATCH 3/3] forgot remove fixme messages --- src/identities.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/identities.rs b/src/identities.rs index 0360a19..36846f2 100644 --- a/src/identities.rs +++ b/src/identities.rs @@ -17,7 +17,6 @@ pub trait Zero: Sized + Add { /// This function should return the same result at all times regardless of /// external mutable state, for example values stored in TLS or in /// `static mut`s. - // FIXME (#5527): This should be an associated constant const ZERO: Self; #[inline] @@ -83,7 +82,6 @@ pub trait One: Sized + Mul { /// This function should return the same result at all times regardless of /// external mutable state, for example values stored in TLS or in /// `static mut`s. - // FIXME (#5527): This should be an associated constant const ONE: Self; #[inline]