num_traits/ops/
overflowing.rs

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