associated consts

This commit is contained in:
Fabio Krapohl 2018-01-17 13:50:33 +01:00
parent 9c8676e6d5
commit 8634d2fad6
2 changed files with 17 additions and 9 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

@ -18,7 +18,11 @@ pub trait Zero: Sized + Add<Self, Output = Self> {
/// 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 // 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. /// 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())
}
} }
@ -78,12 +83,17 @@ pub trait One: Sized + Mul<Self, Output = Self> {
/// 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 // FIXME (#5527): This should be an associated constant
fn one() -> Self; const ONE: Self;
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.