Implemented Zero, One and Num for Wrapping<T>
This commit is contained in:
parent
6513a5618b
commit
b024e1c326
|
@ -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 + PartialEq> Zero for Wrapping<T> where Self: Add<Output=Self> {
|
||||
fn is_zero(&self) -> bool {
|
||||
self.0 == T::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 Self: Add<Output=Self> + Mul<Output=Self> {
|
||||
fn one() -> Self {
|
||||
Wrapping(T::one())
|
||||
}
|
||||
}
|
||||
|
||||
// Some helper functions provided for backwards compatibility.
|
||||
|
||||
|
|
|
@ -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 Self: Zero + One
|
||||
+ Add<Output = Self> + Sub<Output = Self>
|
||||
+ Mul<Output = Self> + Div<Output = Self> + Rem<Output = Self>
|
||||
{
|
||||
type FromStrRadixErr = T::FromStrRadixErr;
|
||||
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
|
||||
T::from_str_radix(str, radix).map(|x| Wrapping(x))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum FloatErrorKind {
|
||||
Empty,
|
||||
|
|
Loading…
Reference in New Issue