|
| 1 | +use core::ops::{Add, Mul, Sub}; |
| 2 | +#[cfg(has_i128)] |
| 3 | +use core::{i128, u128}; |
| 4 | +use core::{i16, i32, i64, i8, isize}; |
| 5 | +use core::{u16, u32, u64, u8, usize}; |
| 6 | + |
| 7 | +macro_rules! overflowing_impl { |
| 8 | + ($trait_name:ident, $method:ident, $t:ty) => { |
| 9 | + impl $trait_name for $t { |
| 10 | + #[inline] |
| 11 | + fn $method(&self, v: &Self) -> (Self, bool) { |
| 12 | + <$t>::$method(*self, *v) |
| 13 | + } |
| 14 | + } |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +/// Performs addition with a flag for overflow. |
| 19 | +pub trait OverflowingAdd: Sized + Add<Self, Output = Self> { |
| 20 | + /// Returns a tuple of the addition along with a boolean indicating whether an arithmetic overflow would occur. |
| 21 | + /// If an overflow would have occurred then the wrapped value is returned. |
| 22 | + fn overflowing_add(&self, v: &Self) -> (Self, bool); |
| 23 | +} |
| 24 | + |
| 25 | +overflowing_impl!(OverflowingAdd, overflowing_add, u8); |
| 26 | +overflowing_impl!(OverflowingAdd, overflowing_add, u16); |
| 27 | +overflowing_impl!(OverflowingAdd, overflowing_add, u32); |
| 28 | +overflowing_impl!(OverflowingAdd, overflowing_add, u64); |
| 29 | +overflowing_impl!(OverflowingAdd, overflowing_add, usize); |
| 30 | +#[cfg(has_i128)] |
| 31 | +overflowing_impl!(OverflowingAdd, overflowing_add, u128); |
| 32 | + |
| 33 | +overflowing_impl!(OverflowingAdd, overflowing_add, i8); |
| 34 | +overflowing_impl!(OverflowingAdd, overflowing_add, i16); |
| 35 | +overflowing_impl!(OverflowingAdd, overflowing_add, i32); |
| 36 | +overflowing_impl!(OverflowingAdd, overflowing_add, i64); |
| 37 | +overflowing_impl!(OverflowingAdd, overflowing_add, isize); |
| 38 | +#[cfg(has_i128)] |
| 39 | +overflowing_impl!(OverflowingAdd, overflowing_add, i128); |
| 40 | + |
| 41 | +/// Performs substraction with a flag for overflow. |
| 42 | +pub trait OverflowingSub: Sized + Sub<Self, Output = Self> { |
| 43 | + /// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow would occur. |
| 44 | + /// If an overflow would have occurred then the wrapped value is returned. |
| 45 | + fn overflowing_sub(&self, v: &Self) -> (Self, bool); |
| 46 | +} |
| 47 | + |
| 48 | +overflowing_impl!(OverflowingSub, overflowing_sub, u8); |
| 49 | +overflowing_impl!(OverflowingSub, overflowing_sub, u16); |
| 50 | +overflowing_impl!(OverflowingSub, overflowing_sub, u32); |
| 51 | +overflowing_impl!(OverflowingSub, overflowing_sub, u64); |
| 52 | +overflowing_impl!(OverflowingSub, overflowing_sub, usize); |
| 53 | +#[cfg(has_i128)] |
| 54 | +overflowing_impl!(OverflowingSub, overflowing_sub, u128); |
| 55 | + |
| 56 | +overflowing_impl!(OverflowingSub, overflowing_sub, i8); |
| 57 | +overflowing_impl!(OverflowingSub, overflowing_sub, i16); |
| 58 | +overflowing_impl!(OverflowingSub, overflowing_sub, i32); |
| 59 | +overflowing_impl!(OverflowingSub, overflowing_sub, i64); |
| 60 | +overflowing_impl!(OverflowingSub, overflowing_sub, isize); |
| 61 | +#[cfg(has_i128)] |
| 62 | +overflowing_impl!(OverflowingSub, overflowing_sub, i128); |
| 63 | + |
| 64 | +/// Performs multiplication with a flag for overflow. |
| 65 | +pub trait OverflowingMul: Sized + Mul<Self, Output = Self> { |
| 66 | + /// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow would occur. |
| 67 | + /// If an overflow would have occurred then the wrapped value is returned. |
| 68 | + fn overflowing_mul(&self, v: &Self) -> (Self, bool); |
| 69 | +} |
| 70 | + |
| 71 | +overflowing_impl!(OverflowingMul, overflowing_mul, u8); |
| 72 | +overflowing_impl!(OverflowingMul, overflowing_mul, u16); |
| 73 | +overflowing_impl!(OverflowingMul, overflowing_mul, u32); |
| 74 | +overflowing_impl!(OverflowingMul, overflowing_mul, u64); |
| 75 | +overflowing_impl!(OverflowingMul, overflowing_mul, usize); |
| 76 | +#[cfg(has_i128)] |
| 77 | +overflowing_impl!(OverflowingMul, overflowing_mul, u128); |
| 78 | + |
| 79 | +overflowing_impl!(OverflowingMul, overflowing_mul, i8); |
| 80 | +overflowing_impl!(OverflowingMul, overflowing_mul, i16); |
| 81 | +overflowing_impl!(OverflowingMul, overflowing_mul, i32); |
| 82 | +overflowing_impl!(OverflowingMul, overflowing_mul, i64); |
| 83 | +overflowing_impl!(OverflowingMul, overflowing_mul, isize); |
| 84 | +#[cfg(has_i128)] |
| 85 | +overflowing_impl!(OverflowingMul, overflowing_mul, i128); |
| 86 | + |
| 87 | +#[test] |
| 88 | +fn test_overflowing_traits() { |
| 89 | + fn overflowing_add<T: OverflowingAdd>(a: T, b: T) -> (T, bool) { |
| 90 | + a.overflowing_add(&b) |
| 91 | + } |
| 92 | + fn overflowing_sub<T: OverflowingSub>(a: T, b: T) -> (T, bool) { |
| 93 | + a.overflowing_sub(&b) |
| 94 | + } |
| 95 | + fn overflowing_mul<T: OverflowingMul>(a: T, b: T) -> (T, bool) { |
| 96 | + a.overflowing_mul(&b) |
| 97 | + } |
| 98 | + assert_eq!(overflowing_add(5i16, 2), (7, false)); |
| 99 | + assert_eq!(overflowing_add(i16::MAX, 1), (i16::MIN, true)); |
| 100 | + assert_eq!(overflowing_sub(5i16, 2), (3, false)); |
| 101 | + assert_eq!(overflowing_sub(i16::MIN, 1), (i16::MAX, true)); |
| 102 | + assert_eq!(overflowing_mul(5i16, 2), (10, false)); |
| 103 | + assert_eq!(overflowing_mul(1_000_000_000i32, 10), (1410065408, true)); |
| 104 | +} |
0 commit comments