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
23overflowing_impl!(OverflowingAdd, overflowing_add, u8);
24overflowing_impl!(OverflowingAdd, overflowing_add, u16);
25overflowing_impl!(OverflowingAdd, overflowing_add, u32);
26overflowing_impl!(OverflowingAdd, overflowing_add, u64);
27overflowing_impl!(OverflowingAdd, overflowing_add, usize);
28overflowing_impl!(OverflowingAdd, overflowing_add, u128);
29
30overflowing_impl!(OverflowingAdd, overflowing_add, i8);
31overflowing_impl!(OverflowingAdd, overflowing_add, i16);
32overflowing_impl!(OverflowingAdd, overflowing_add, i32);
33overflowing_impl!(OverflowingAdd, overflowing_add, i64);
34overflowing_impl!(OverflowingAdd, overflowing_add, isize);
35overflowing_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
44overflowing_impl!(OverflowingSub, overflowing_sub, u8);
45overflowing_impl!(OverflowingSub, overflowing_sub, u16);
46overflowing_impl!(OverflowingSub, overflowing_sub, u32);
47overflowing_impl!(OverflowingSub, overflowing_sub, u64);
48overflowing_impl!(OverflowingSub, overflowing_sub, usize);
49overflowing_impl!(OverflowingSub, overflowing_sub, u128);
50
51overflowing_impl!(OverflowingSub, overflowing_sub, i8);
52overflowing_impl!(OverflowingSub, overflowing_sub, i16);
53overflowing_impl!(OverflowingSub, overflowing_sub, i32);
54overflowing_impl!(OverflowingSub, overflowing_sub, i64);
55overflowing_impl!(OverflowingSub, overflowing_sub, isize);
56overflowing_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
65overflowing_impl!(OverflowingMul, overflowing_mul, u8);
66overflowing_impl!(OverflowingMul, overflowing_mul, u16);
67overflowing_impl!(OverflowingMul, overflowing_mul, u32);
68overflowing_impl!(OverflowingMul, overflowing_mul, u64);
69overflowing_impl!(OverflowingMul, overflowing_mul, usize);
70overflowing_impl!(OverflowingMul, overflowing_mul, u128);
71
72overflowing_impl!(OverflowingMul, overflowing_mul, i8);
73overflowing_impl!(OverflowingMul, overflowing_mul, i16);
74overflowing_impl!(OverflowingMul, overflowing_mul, i32);
75overflowing_impl!(OverflowingMul, overflowing_mul, i64);
76overflowing_impl!(OverflowingMul, overflowing_mul, isize);
77overflowing_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}
97