1 | use core::ops::{Add, Mul, Sub}; |
---|---|

2 | use core::{i128, i16, i32, i64, i8, isize}; |

3 | use core::{u128, u16, u32, u64, u8, usize}; |

4 | |

5 | macro_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. |

17 | pub 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 | |

23 | overflowing_impl!(OverflowingAdd, overflowing_add, u8); |

24 | overflowing_impl!(OverflowingAdd, overflowing_add, u16); |

25 | overflowing_impl!(OverflowingAdd, overflowing_add, u32); |

26 | overflowing_impl!(OverflowingAdd, overflowing_add, u64); |

27 | overflowing_impl!(OverflowingAdd, overflowing_add, usize); |

28 | overflowing_impl!(OverflowingAdd, overflowing_add, u128); |

29 | |

30 | overflowing_impl!(OverflowingAdd, overflowing_add, i8); |

31 | overflowing_impl!(OverflowingAdd, overflowing_add, i16); |

32 | overflowing_impl!(OverflowingAdd, overflowing_add, i32); |

33 | overflowing_impl!(OverflowingAdd, overflowing_add, i64); |

34 | overflowing_impl!(OverflowingAdd, overflowing_add, isize); |

35 | overflowing_impl!(OverflowingAdd, overflowing_add, i128); |

36 | |

37 | /// Performs substraction with a flag for overflow. |

38 | pub 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 | |

44 | overflowing_impl!(OverflowingSub, overflowing_sub, u8); |

45 | overflowing_impl!(OverflowingSub, overflowing_sub, u16); |

46 | overflowing_impl!(OverflowingSub, overflowing_sub, u32); |

47 | overflowing_impl!(OverflowingSub, overflowing_sub, u64); |

48 | overflowing_impl!(OverflowingSub, overflowing_sub, usize); |

49 | overflowing_impl!(OverflowingSub, overflowing_sub, u128); |

50 | |

51 | overflowing_impl!(OverflowingSub, overflowing_sub, i8); |

52 | overflowing_impl!(OverflowingSub, overflowing_sub, i16); |

53 | overflowing_impl!(OverflowingSub, overflowing_sub, i32); |

54 | overflowing_impl!(OverflowingSub, overflowing_sub, i64); |

55 | overflowing_impl!(OverflowingSub, overflowing_sub, isize); |

56 | overflowing_impl!(OverflowingSub, overflowing_sub, i128); |

57 | |

58 | /// Performs multiplication with a flag for overflow. |

59 | pub 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 | |

65 | overflowing_impl!(OverflowingMul, overflowing_mul, u8); |

66 | overflowing_impl!(OverflowingMul, overflowing_mul, u16); |

67 | overflowing_impl!(OverflowingMul, overflowing_mul, u32); |

68 | overflowing_impl!(OverflowingMul, overflowing_mul, u64); |

69 | overflowing_impl!(OverflowingMul, overflowing_mul, usize); |

70 | overflowing_impl!(OverflowingMul, overflowing_mul, u128); |

71 | |

72 | overflowing_impl!(OverflowingMul, overflowing_mul, i8); |

73 | overflowing_impl!(OverflowingMul, overflowing_mul, i16); |

74 | overflowing_impl!(OverflowingMul, overflowing_mul, i32); |

75 | overflowing_impl!(OverflowingMul, overflowing_mul, i64); |

76 | overflowing_impl!(OverflowingMul, overflowing_mul, isize); |

77 | overflowing_impl!(OverflowingMul, overflowing_mul, i128); |

78 | |

79 | #[test] |

80 | fn 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 |