Merge pull request #256 from nwin/add-wrapping-traits
Added traits for wrapping arithmetics.
This commit is contained in:
commit
817ab00f64
|
@ -20,6 +20,7 @@ pub use bounds::Bounded;
|
|||
pub use float::{Float, FloatConst};
|
||||
pub use identities::{Zero, One, zero, one};
|
||||
pub use ops::checked::*;
|
||||
pub use ops::wrapping::*;
|
||||
pub use ops::saturating::Saturating;
|
||||
pub use sign::{Signed, Unsigned, abs, abs_sub, signum};
|
||||
pub use cast::*;
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
pub mod saturating;
|
||||
pub mod checked;
|
||||
pub mod wrapping;
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
use std::ops::{Add, Sub, Mul};
|
||||
|
||||
macro_rules! wrapping_impl {
|
||||
($trait_name:ident, $method:ident, $t:ty) => {
|
||||
impl $trait_name for $t {
|
||||
#[inline]
|
||||
fn $method(&self, v: &Self) -> Self {
|
||||
<$t>::$method(*self, *v)
|
||||
}
|
||||
}
|
||||
};
|
||||
($trait_name:ident, $method:ident, $t:ty, $rhs:ty) => {
|
||||
impl $trait_name<$rhs> for $t {
|
||||
#[inline]
|
||||
fn $method(&self, v: &$rhs) -> Self {
|
||||
<$t>::$method(*self, *v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Performs addition that wraps around on overflow.
|
||||
pub trait WrappingAdd: Sized + Add<Self, Output=Self> {
|
||||
/// Wrapping (modular) addition. Computes `self + other`, wrapping around at the boundary of
|
||||
/// the type.
|
||||
fn wrapping_add(&self, v: &Self) -> Self;
|
||||
}
|
||||
|
||||
wrapping_impl!(WrappingAdd, wrapping_add, u8);
|
||||
wrapping_impl!(WrappingAdd, wrapping_add, u16);
|
||||
wrapping_impl!(WrappingAdd, wrapping_add, u32);
|
||||
wrapping_impl!(WrappingAdd, wrapping_add, u64);
|
||||
wrapping_impl!(WrappingAdd, wrapping_add, usize);
|
||||
|
||||
wrapping_impl!(WrappingAdd, wrapping_add, i8);
|
||||
wrapping_impl!(WrappingAdd, wrapping_add, i16);
|
||||
wrapping_impl!(WrappingAdd, wrapping_add, i32);
|
||||
wrapping_impl!(WrappingAdd, wrapping_add, i64);
|
||||
wrapping_impl!(WrappingAdd, wrapping_add, isize);
|
||||
|
||||
/// Performs subtraction that wraps around on overflow.
|
||||
pub trait WrappingSub: Sized + Sub<Self, Output=Self> {
|
||||
/// Wrapping (modular) subtraction. Computes `self - other`, wrapping around at the boundary
|
||||
/// of the type.
|
||||
fn wrapping_sub(&self, v: &Self) -> Self;
|
||||
}
|
||||
|
||||
wrapping_impl!(WrappingSub, wrapping_sub, u8);
|
||||
wrapping_impl!(WrappingSub, wrapping_sub, u16);
|
||||
wrapping_impl!(WrappingSub, wrapping_sub, u32);
|
||||
wrapping_impl!(WrappingSub, wrapping_sub, u64);
|
||||
wrapping_impl!(WrappingSub, wrapping_sub, usize);
|
||||
|
||||
wrapping_impl!(WrappingSub, wrapping_sub, i8);
|
||||
wrapping_impl!(WrappingSub, wrapping_sub, i16);
|
||||
wrapping_impl!(WrappingSub, wrapping_sub, i32);
|
||||
wrapping_impl!(WrappingSub, wrapping_sub, i64);
|
||||
wrapping_impl!(WrappingSub, wrapping_sub, isize);
|
||||
|
||||
/// Performs multiplication that wraps around on overflow.
|
||||
pub trait WrappingMul: Sized + Mul<Self, Output=Self> {
|
||||
/// Wrapping (modular) multiplication. Computes `self * other`, wrapping around at the boundary
|
||||
/// of the type.
|
||||
fn wrapping_mul(&self, v: &Self) -> Self;
|
||||
}
|
||||
|
||||
wrapping_impl!(WrappingMul, wrapping_mul, u8);
|
||||
wrapping_impl!(WrappingMul, wrapping_mul, u16);
|
||||
wrapping_impl!(WrappingMul, wrapping_mul, u32);
|
||||
wrapping_impl!(WrappingMul, wrapping_mul, u64);
|
||||
wrapping_impl!(WrappingMul, wrapping_mul, usize);
|
||||
|
||||
wrapping_impl!(WrappingMul, wrapping_mul, i8);
|
||||
wrapping_impl!(WrappingMul, wrapping_mul, i16);
|
||||
wrapping_impl!(WrappingMul, wrapping_mul, i32);
|
||||
wrapping_impl!(WrappingMul, wrapping_mul, i64);
|
||||
wrapping_impl!(WrappingMul, wrapping_mul, isize);
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_wrapping_traits() {
|
||||
fn wrapping_add<T: WrappingAdd>(a: T, b: T) -> T { a.wrapping_add(&b) }
|
||||
fn wrapping_sub<T: WrappingSub>(a: T, b: T) -> T { a.wrapping_sub(&b) }
|
||||
fn wrapping_mul<T: WrappingMul>(a: T, b: T) -> T { a.wrapping_mul(&b) }
|
||||
assert_eq!(wrapping_add(255, 1), 0u8);
|
||||
assert_eq!(wrapping_sub(0, 1), 255u8);
|
||||
assert_eq!(wrapping_mul(255, 2), 254u8);
|
||||
}
|
Loading…
Reference in New Issue