This commit is contained in:
Fabio Krapohl 2018-01-18 07:13:02 +00:00 committed by GitHub
commit 789dea0d5f
3 changed files with 36 additions and 29 deletions

View File

@ -8,7 +8,7 @@ categories = [ "algorithms", "science" ]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
repository = "https://github.com/rust-num/num-traits" repository = "https://github.com/rust-num/num-traits"
name = "num-traits" name = "num-traits"
version = "0.1.41" version = "0.2.0"
readme = "README.md" readme = "README.md"
[dependencies] [dependencies]

View File

@ -5,21 +5,25 @@ use std::num::Wrapping;
/// Numbers which have upper and lower bounds /// Numbers which have upper and lower bounds
pub trait Bounded { pub trait Bounded {
// FIXME (#5527): These should be associated constants
/// returns the smallest finite number this type can represent /// 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 /// 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 { macro_rules! bounded_impl {
($t:ty, $min:expr, $max:expr) => { ($t:ty, $min:expr, $max:expr) => {
impl Bounded for $t { impl Bounded for $t {
#[inline] const MIN: $t = $min;
fn min_value() -> $t { $min } const MAX: $t = $max;
#[inline]
fn max_value() -> $t { $max }
} }
} }
} }
@ -37,8 +41,9 @@ bounded_impl!(i32, i32::MIN, i32::MAX);
bounded_impl!(i64, i64::MIN, i64::MAX); bounded_impl!(i64, i64::MIN, i64::MAX);
impl<T: Bounded> Bounded for Wrapping<T> { impl<T: Bounded> Bounded for Wrapping<T> {
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); bounded_impl!(f32, f32::MIN, f32::MAX);
@ -61,14 +66,8 @@ macro_rules! for_each_tuple {
macro_rules! bounded_tuple { macro_rules! bounded_tuple {
( $($name:ident)* ) => ( ( $($name:ident)* ) => (
impl<$($name: Bounded,)*> Bounded for ($($name,)*) { impl<$($name: Bounded,)*> Bounded for ($($name,)*) {
#[inline] const MIN: Self = ($($name::MIN,)*);
fn min_value() -> Self { const MAX: Self = ($($name::MAX,)*);
($($name::min_value(),)*)
}
#[inline]
fn max_value() -> Self {
($($name::max_value(),)*)
}
} }
); );
} }

View File

@ -17,8 +17,12 @@ pub trait Zero: Sized + Add<Self, Output = Self> {
/// This function should return the same result at all times regardless of /// This function should return the same result at all times regardless of
/// external mutable state, for example values stored in TLS or in /// external mutable state, for example values stored in TLS or in
/// `static mut`s. /// `static mut`s.
// FIXME (#5527): This should be an associated constant const ZERO: Self;
fn zero() -> Self;
#[inline]
fn zero() -> Self {
Self::ZERO
}
/// Returns `true` if `self` is equal to the additive identity. /// Returns `true` if `self` is equal to the additive identity.
#[inline] #[inline]
@ -28,6 +32,7 @@ pub trait Zero: Sized + Add<Self, Output = Self> {
macro_rules! zero_impl { macro_rules! zero_impl {
($t:ty, $v:expr) => { ($t:ty, $v:expr) => {
impl Zero for $t { impl Zero for $t {
const ZERO: $t = $v;
#[inline] #[inline]
fn zero() -> $t { $v } fn zero() -> $t { $v }
#[inline] #[inline]
@ -52,12 +57,12 @@ zero_impl!(f32, 0.0f32);
zero_impl!(f64, 0.0f64); zero_impl!(f64, 0.0f64);
impl<T: Zero> Zero for Wrapping<T> where Wrapping<T>: Add<Output=Wrapping<T>> { impl<T: Zero> Zero for Wrapping<T> where Wrapping<T>: Add<Output=Wrapping<T>> {
fn is_zero(&self) -> bool { fn is_zero(&self) -> bool {
self.0.is_zero() self.0.is_zero()
} }
fn zero() -> Self { const ZERO: Self = Wrapping(T::ZERO);
Wrapping(T::zero())
}
} }
@ -77,13 +82,18 @@ pub trait One: Sized + Mul<Self, Output = Self> {
/// This function should return the same result at all times regardless of /// This function should return the same result at all times regardless of
/// external mutable state, for example values stored in TLS or in /// external mutable state, for example values stored in TLS or in
/// `static mut`s. /// `static mut`s.
// FIXME (#5527): This should be an associated constant const ONE: Self;
fn one() -> Self;
#[inline]
fn one() -> Self {
Self::ONE
}
} }
macro_rules! one_impl { macro_rules! one_impl {
($t:ty, $v:expr) => { ($t:ty, $v:expr) => {
impl One for $t { impl One for $t {
const ONE: $t = $v;
#[inline] #[inline]
fn one() -> $t { $v } fn one() -> $t { $v }
} }
@ -106,9 +116,7 @@ one_impl!(f32, 1.0f32);
one_impl!(f64, 1.0f64); one_impl!(f64, 1.0f64);
impl<T: One> One for Wrapping<T> where Wrapping<T>: Mul<Output=Wrapping<T>> { impl<T: One> One for Wrapping<T> where Wrapping<T>: Mul<Output=Wrapping<T>> {
fn one() -> Self { const ONE: Self = Wrapping(T::ONE);
Wrapping(T::one())
}
} }
// Some helper functions provided for backwards compatibility. // Some helper functions provided for backwards compatibility.