Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions traits/src/identities.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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.

Expand Down
19 changes: 16 additions & 3 deletions traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea what caused these changes - didn't do these on purpose anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess your editor is set to strip trailing whitespace. That's OK.

/// 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");
Expand Down