Skip to content

Commit e2ea246

Browse files
committed
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
2 parents 6513a56 + 2172a93 commit e2ea246

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

traits/src/identities.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::ops::{Add, Mul};
2+
use std::num::Wrapping;
23

34
/// Defines an additive identity element for `Self`.
45
pub trait Zero: Sized + Add<Self, Output = Self> {
@@ -50,6 +51,16 @@ zero_impl!(i64, 0i64);
5051
zero_impl!(f32, 0.0f32);
5152
zero_impl!(f64, 0.0f64);
5253

54+
impl<T: Zero> Zero for Wrapping<T> where Wrapping<T>: Add<Output=Wrapping<T>> {
55+
fn is_zero(&self) -> bool {
56+
self.0.is_zero()
57+
}
58+
fn zero() -> Self {
59+
Wrapping(T::zero())
60+
}
61+
}
62+
63+
5364
/// Defines a multiplicative identity element for `Self`.
5465
pub trait One: Sized + Mul<Self, Output = Self> {
5566
/// Returns the multiplicative identity element of `Self`, `1`.
@@ -94,6 +105,11 @@ one_impl!(i64, 1i64);
94105
one_impl!(f32, 1.0f32);
95106
one_impl!(f64, 1.0f64);
96107

108+
impl<T: One> One for Wrapping<T> where Wrapping<T>: Mul<Output=Wrapping<T>> {
109+
fn one() -> Self {
110+
Wrapping(T::one())
111+
}
112+
}
97113

98114
// Some helper functions provided for backwards compatibility.
99115

traits/src/lib.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
html_playground_url = "http://play.integer32.com/")]
1616

1717
use std::ops::{Add, Sub, Mul, Div, Rem};
18+
use std::num::Wrapping;
1819

1920
pub use bounds::Bounded;
2021
pub use float::{Float, FloatConst};
@@ -74,6 +75,18 @@ macro_rules! int_trait_impl {
7475
}
7576
int_trait_impl!(Num for usize u8 u16 u32 u64 isize i8 i16 i32 i64);
7677

78+
impl<T: Num> Num for Wrapping<T>
79+
where Wrapping<T>:
80+
Add<Output = Wrapping<T>> + Sub<Output = Wrapping<T>>
81+
+ Mul<Output = Wrapping<T>> + Div<Output = Wrapping<T>> + Rem<Output = Wrapping<T>>
82+
{
83+
type FromStrRadixErr = T::FromStrRadixErr;
84+
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
85+
T::from_str_radix(str, radix).map(Wrapping)
86+
}
87+
}
88+
89+
7790
#[derive(Debug)]
7891
pub enum FloatErrorKind {
7992
Empty,
@@ -243,9 +256,9 @@ float_trait_impl!(Num for f32 f64);
243256

244257
/// A value bounded by a minimum and a maximum
245258
///
246-
/// If input is less than min then this returns min.
247-
/// If input is greater than max then this returns max.
248-
/// Otherwise this returns input.
259+
/// If input is less than min then this returns min.
260+
/// If input is greater than max then this returns max.
261+
/// Otherwise this returns input.
249262
#[inline]
250263
pub fn clamp<T: PartialOrd>(input: T, min: T, max: T) -> T {
251264
debug_assert!(min <= max, "min must be less than or equal to max");

0 commit comments

Comments
 (0)