Auto merge of #279 - yoanlcq:impl-num-for-wrapping, r=cuviper

Implemented Zero, One and Num for std::num::Wrapping<T>

Attempts to fix issue #278
This commit is contained in:
Homu 2017-04-22 02:18:42 +09:00
commit e2ea246016
2 changed files with 32 additions and 3 deletions

View File

@ -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<Self, Output = Self> {
@ -50,6 +51,16 @@ zero_impl!(i64, 0i64);
zero_impl!(f32, 0.0f32);
zero_impl!(f64, 0.0f64);
impl<T: Zero> Zero for Wrapping<T> where Wrapping<T>: Add<Output=Wrapping<T>> {
fn is_zero(&self) -> bool {
self.0.is_zero()
}
fn zero() -> Self {
Wrapping(T::zero())
}
}
/// Defines a multiplicative identity element for `Self`.
pub trait One: Sized + Mul<Self, Output = Self> {
/// 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<T: One> One for Wrapping<T> where Wrapping<T>: Mul<Output=Wrapping<T>> {
fn one() -> Self {
Wrapping(T::one())
}
}
// Some helper functions provided for backwards compatibility.

View File

@ -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<T: Num> Num for Wrapping<T>
where Wrapping<T>:
Add<Output = Wrapping<T>> + Sub<Output = Wrapping<T>>
+ Mul<Output = Wrapping<T>> + Div<Output = Wrapping<T>> + Rem<Output = Wrapping<T>>
{
type FromStrRadixErr = T::FromStrRadixErr;
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
T::from_str_radix(str, radix).map(Wrapping)
}
}
#[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<T: PartialOrd>(input: T, min: T, max: T) -> T {
debug_assert!(min <= max, "min must be less than or equal to max");