overflowing.rs 4.3 KB

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