Skip to content

Commit

Permalink
Clean up some trait impls in core::num.
Browse files Browse the repository at this point in the history
This removes the special casing for `float`s where it was not necessary, as
`-0.0 == 0.0`.
  • Loading branch information
tbu- committed Jul 22, 2014
1 parent 915fb2b commit 737d92e
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub trait Zero: Add<Self, Self> {
fn zero() -> Self;

/// Returns `true` if `self` is equal to the additive identity.
#[inline]
fn is_zero(&self) -> bool;
}

Expand All @@ -89,32 +90,20 @@ macro_rules! zero_impl(
}
)

macro_rules! zero_float_impl(
($t:ty, $v:expr) => {
impl Zero for $t {
#[inline]
fn zero() -> $t { $v }

#[inline]
fn is_zero(&self) -> bool { *self == $v || *self == -$v }
}
}
)

zero_impl!(uint, 0u)
zero_impl!(u8, 0u8)
zero_impl!(u16, 0u16)
zero_impl!(u32, 0u32)
zero_impl!(u64, 0u64)
zero_impl!(u8, 0u8)
zero_impl!(u16, 0u16)
zero_impl!(u32, 0u32)
zero_impl!(u64, 0u64)

zero_impl!(int, 0i)
zero_impl!(i8, 0i8)
zero_impl!(i16, 0i16)
zero_impl!(i32, 0i32)
zero_impl!(i64, 0i64)

zero_float_impl!(f32, 0.0f32)
zero_float_impl!(f64, 0.0f64)
zero_impl!(f32, 0.0f32)
zero_impl!(f64, 0.0f64)

/// Returns the additive identity, `0`.
#[inline(always)] pub fn zero<T: Zero>() -> T { Zero::zero() }
Expand Down

0 comments on commit 737d92e

Please sign in to comment.