From ae63b9eeca1a112ba851024350a4902a38373539 Mon Sep 17 00:00:00 2001 From: Kamil Niski Date: Fri, 17 Jul 2020 08:18:13 +0200 Subject: [PATCH 1/3] Add Overflowing trait Closes issue #168 --- src/ops/mod.rs | 1 + src/ops/overflowing.rs | 97 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/ops/overflowing.rs diff --git a/src/ops/mod.rs b/src/ops/mod.rs index fd1695d9..73c4f902 100644 --- a/src/ops/mod.rs +++ b/src/ops/mod.rs @@ -1,5 +1,6 @@ pub mod checked; pub mod inv; pub mod mul_add; +pub mod overflowing; pub mod saturating; pub mod wrapping; diff --git a/src/ops/overflowing.rs b/src/ops/overflowing.rs new file mode 100644 index 00000000..e8bd9e3b --- /dev/null +++ b/src/ops/overflowing.rs @@ -0,0 +1,97 @@ +use core::ops::{Add, Mul, Sub}; + +macro_rules! overflowing_impl { + ($trait_name:ident, $method:ident, $t:ty) => { + impl $trait_name for $t { + #[inline] + fn $method(&self, v: &Self) -> (Self, bool) { + <$t>::$method(*self, *v) + } + } + }; +} + +/// If an overflow would have occurred then the wrapped value is returned. +pub trait OverflowingAdd: Sized + Add { + /// Returns a tuple of the addition along with a boolean indicating whether an arithmetic overflow would occur. + fn overflowing_add(&self, v: &Self) -> (Self, bool); +} + +overflowing_impl!(OverflowingAdd, overflowing_add, u8); +overflowing_impl!(OverflowingAdd, overflowing_add, u16); +overflowing_impl!(OverflowingAdd, overflowing_add, u32); +overflowing_impl!(OverflowingAdd, overflowing_add, u64); +overflowing_impl!(OverflowingAdd, overflowing_add, usize); +#[cfg(has_i128)] +overflowing_impl!(OverflowingAdd, overflowing_add, u128); + +overflowing_impl!(OverflowingAdd, overflowing_add, i8); +overflowing_impl!(OverflowingAdd, overflowing_add, i16); +overflowing_impl!(OverflowingAdd, overflowing_add, i32); +overflowing_impl!(OverflowingAdd, overflowing_add, i64); +overflowing_impl!(OverflowingAdd, overflowing_add, isize); +#[cfg(has_i128)] +overflowing_impl!(OverflowingAdd, overflowing_add, i128); + +/// If an overflow would have occurred then the wrapped value is returned. +pub trait OverflowingSub: Sized + Sub { + /// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow would occur. + fn overflowing_sub(&self, v: &Self) -> (Self, bool); +} + +overflowing_impl!(OverflowingSub, overflowing_sub, u8); +overflowing_impl!(OverflowingSub, overflowing_sub, u16); +overflowing_impl!(OverflowingSub, overflowing_sub, u32); +overflowing_impl!(OverflowingSub, overflowing_sub, u64); +overflowing_impl!(OverflowingSub, overflowing_sub, usize); +#[cfg(has_i128)] +overflowing_impl!(OverflowingSub, overflowing_sub, u128); + +overflowing_impl!(OverflowingSub, overflowing_sub, i8); +overflowing_impl!(OverflowingSub, overflowing_sub, i16); +overflowing_impl!(OverflowingSub, overflowing_sub, i32); +overflowing_impl!(OverflowingSub, overflowing_sub, i64); +overflowing_impl!(OverflowingSub, overflowing_sub, isize); +#[cfg(has_i128)] +overflowing_impl!(OverflowingSub, overflowing_sub, i128); + +/// If an overflow would have occurred then the wrapped value is returned. +pub trait OverflowingMul: Sized + Mul { + /// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow would occur. + fn overflowing_mul(&self, v: &Self) -> (Self, bool); +} + +overflowing_impl!(OverflowingMul, overflowing_mul, u8); +overflowing_impl!(OverflowingMul, overflowing_mul, u16); +overflowing_impl!(OverflowingMul, overflowing_mul, u32); +overflowing_impl!(OverflowingMul, overflowing_mul, u64); +overflowing_impl!(OverflowingMul, overflowing_mul, usize); +#[cfg(has_i128)] +overflowing_impl!(OverflowingMul, overflowing_mul, u128); + +overflowing_impl!(OverflowingMul, overflowing_mul, i8); +overflowing_impl!(OverflowingMul, overflowing_mul, i16); +overflowing_impl!(OverflowingMul, overflowing_mul, i32); +overflowing_impl!(OverflowingMul, overflowing_mul, i64); +overflowing_impl!(OverflowingMul, overflowing_mul, isize); +#[cfg(has_i128)] +overflowing_impl!(OverflowingMul, overflowing_mul, i128); + +#[test] +fn test_overflowing_traits() { + fn overflowing_add(a: T, b: T) -> (T, bool) { + a.overflowing_add(&b) + } + fn overflowing_sub(a: T, b: T) -> (T, bool) { + a.overflowing_sub(&b) + } + fn overflowing_mul(a: T, b: T) -> (T, bool) { + a.overflowing_mul(&b) + } + assert_eq!(overflowing_add(5i16, 2), (7, false)); + assert_eq!(overflowing_add(std::i16::MAX, 1), (std::i16::MIN, true)); + assert_eq!(overflowing_sub(5i16, 2), (3, false)); + assert_eq!(overflowing_sub(std::i16::MIN, 1), (std::i16::MAX, true)); + assert_eq!(overflowing_mul(5i16, 2), (10, false)); + assert_eq!(overflowing_mul(1_000_000_000i32, 10), (1410065408, true)); +} From bdf0c1da76f39de7429ddbd33cdf1fd97ac0e715 Mon Sep 17 00:00:00 2001 From: Kamil Niski Date: Fri, 17 Jul 2020 08:33:54 +0200 Subject: [PATCH 2/3] Remove std dep from integer MAX, MIN --- src/ops/overflowing.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ops/overflowing.rs b/src/ops/overflowing.rs index e8bd9e3b..bf0d9a36 100644 --- a/src/ops/overflowing.rs +++ b/src/ops/overflowing.rs @@ -1,4 +1,8 @@ use core::ops::{Add, Mul, Sub}; +#[cfg(has_i128)] +use core::{i128, u128}; +use core::{i16, i32, i64, i8, isize}; +use core::{u16, u32, u64, u8, usize}; macro_rules! overflowing_impl { ($trait_name:ident, $method:ident, $t:ty) => { @@ -89,9 +93,9 @@ fn test_overflowing_traits() { a.overflowing_mul(&b) } assert_eq!(overflowing_add(5i16, 2), (7, false)); - assert_eq!(overflowing_add(std::i16::MAX, 1), (std::i16::MIN, true)); + assert_eq!(overflowing_add(i16::MAX, 1), (i16::MIN, true)); assert_eq!(overflowing_sub(5i16, 2), (3, false)); - assert_eq!(overflowing_sub(std::i16::MIN, 1), (std::i16::MAX, true)); + assert_eq!(overflowing_sub(i16::MIN, 1), (i16::MAX, true)); assert_eq!(overflowing_mul(5i16, 2), (10, false)); assert_eq!(overflowing_mul(1_000_000_000i32, 10), (1410065408, true)); } From a939c51793a54d35c6f9d2ad60a6fcfd43904d8b Mon Sep 17 00:00:00 2001 From: Kamil Niski Date: Sat, 24 Oct 2020 14:36:38 +0200 Subject: [PATCH 3/3] Correct docs for overflowing docs --- src/ops/overflowing.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ops/overflowing.rs b/src/ops/overflowing.rs index bf0d9a36..ff97ea54 100644 --- a/src/ops/overflowing.rs +++ b/src/ops/overflowing.rs @@ -15,9 +15,10 @@ macro_rules! overflowing_impl { }; } -/// If an overflow would have occurred then the wrapped value is returned. +/// Performs addition with a flag for overflow. pub trait OverflowingAdd: Sized + Add { /// Returns a tuple of the addition along with a boolean indicating whether an arithmetic overflow would occur. + /// If an overflow would have occurred then the wrapped value is returned. fn overflowing_add(&self, v: &Self) -> (Self, bool); } @@ -37,9 +38,10 @@ overflowing_impl!(OverflowingAdd, overflowing_add, isize); #[cfg(has_i128)] overflowing_impl!(OverflowingAdd, overflowing_add, i128); -/// If an overflow would have occurred then the wrapped value is returned. +/// Performs substraction with a flag for overflow. pub trait OverflowingSub: Sized + Sub { /// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow would occur. + /// If an overflow would have occurred then the wrapped value is returned. fn overflowing_sub(&self, v: &Self) -> (Self, bool); } @@ -59,9 +61,10 @@ overflowing_impl!(OverflowingSub, overflowing_sub, isize); #[cfg(has_i128)] overflowing_impl!(OverflowingSub, overflowing_sub, i128); -/// If an overflow would have occurred then the wrapped value is returned. +/// Performs multiplication with a flag for overflow. pub trait OverflowingMul: Sized + Mul { /// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow would occur. + /// If an overflow would have occurred then the wrapped value is returned. fn overflowing_mul(&self, v: &Self) -> (Self, bool); }