|
| 1 | +mod iter; |
| 2 | + |
| 3 | +pub use iter::*; |
| 4 | + |
| 5 | +pub trait SafeArith<Rhs = Self>: Sized + Copy { |
| 6 | + const ZERO: Self; |
| 7 | + const ONE: Self; |
| 8 | + |
| 9 | + /// Safe variant of `+` that guards against overflow. |
| 10 | + fn safe_add(&self, other: Rhs) -> Result<Self>; |
| 11 | + |
| 12 | + /// Safe variant of `-` that guards against overflow. |
| 13 | + fn safe_sub(&self, other: Rhs) -> Result<Self>; |
| 14 | + |
| 15 | + /// Safe variant of `*` that guards against overflow. |
| 16 | + fn safe_mul(&self, other: Rhs) -> Result<Self>; |
| 17 | + |
| 18 | + /// Safe variant of `/` that guards against division by 0. |
| 19 | + fn safe_div(&self, other: Rhs) -> Result<Self>; |
| 20 | + |
| 21 | + /// Safe variant of `%` that guards against division by 0. |
| 22 | + fn safe_rem(&self, other: Rhs) -> Result<Self>; |
| 23 | + |
| 24 | + /// Safe variant of `<<` that guards against overflow. |
| 25 | + fn safe_shl(&self, other: u32) -> Result<Self>; |
| 26 | + |
| 27 | + /// Safe variant of `>>` that guards against overflow. |
| 28 | + fn safe_shr(&self, other: u32) -> Result<Self>; |
| 29 | + |
| 30 | + /// Safe variant of `+=` that guards against overflow. |
| 31 | + fn safe_add_assign(&mut self, other: Rhs) -> Result<()> { |
| 32 | + *self = self.safe_add(other)?; |
| 33 | + Ok(()) |
| 34 | + } |
| 35 | + |
| 36 | + /// Safe variant of `-=` that guards against overflow. |
| 37 | + fn safe_sub_assign(&mut self, other: Rhs) -> Result<()> { |
| 38 | + *self = self.safe_sub(other)?; |
| 39 | + Ok(()) |
| 40 | + } |
| 41 | + |
| 42 | + /// Safe variant of `*=` that guards against overflow. |
| 43 | + fn safe_mul_assign(&mut self, other: Rhs) -> Result<()> { |
| 44 | + *self = self.safe_mul(other)?; |
| 45 | + Ok(()) |
| 46 | + } |
| 47 | + |
| 48 | + /// Safe variant of `/=` that guards against division by 0. |
| 49 | + fn safe_div_assign(&mut self, other: Rhs) -> Result<()> { |
| 50 | + *self = self.safe_div(other)?; |
| 51 | + Ok(()) |
| 52 | + } |
| 53 | + |
| 54 | + /// Safe variant of `%=` that guards against division by 0. |
| 55 | + fn safe_rem_assign(&mut self, other: Rhs) -> Result<()> { |
| 56 | + *self = self.safe_rem(other)?; |
| 57 | + Ok(()) |
| 58 | + } |
| 59 | + |
| 60 | + /// Safe variant of `<<=` that guards against overflow. |
| 61 | + fn safe_shl_assign(&mut self, other: u32) -> Result<()> { |
| 62 | + *self = self.safe_shl(other)?; |
| 63 | + Ok(()) |
| 64 | + } |
| 65 | + |
| 66 | + /// Safe variant of `>>=` that guards against overflow. |
| 67 | + fn safe_shr_assign(&mut self, other: u32) -> Result<()> { |
| 68 | + *self = self.safe_shr(other)?; |
| 69 | + Ok(()) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +macro_rules! impl_safe_arith { |
| 74 | + ($typ:ty) => { |
| 75 | + impl SafeArith for $typ { |
| 76 | + const ZERO: Self = 0; |
| 77 | + const ONE: Self = 1; |
| 78 | + |
| 79 | + #[inline] |
| 80 | + fn safe_add(&self, other: Self) -> Result<Self> { |
| 81 | + self.checked_add(other).ok_or(ArithError::Overflow) |
| 82 | + } |
| 83 | + |
| 84 | + #[inline] |
| 85 | + fn safe_sub(&self, other: Self) -> Result<Self> { |
| 86 | + self.checked_sub(other).ok_or(ArithError::Overflow) |
| 87 | + } |
| 88 | + |
| 89 | + #[inline] |
| 90 | + fn safe_mul(&self, other: Self) -> Result<Self> { |
| 91 | + self.checked_mul(other).ok_or(ArithError::Overflow) |
| 92 | + } |
| 93 | + |
| 94 | + #[inline] |
| 95 | + fn safe_div(&self, other: Self) -> Result<Self> { |
| 96 | + self.checked_div(other).ok_or(ArithError::DivisionByZero) |
| 97 | + } |
| 98 | + |
| 99 | + #[inline] |
| 100 | + fn safe_rem(&self, other: Self) -> Result<Self> { |
| 101 | + self.checked_rem(other).ok_or(ArithError::DivisionByZero) |
| 102 | + } |
| 103 | + |
| 104 | + #[inline] |
| 105 | + fn safe_shl(&self, other: u32) -> Result<Self> { |
| 106 | + self.checked_shl(other).ok_or(ArithError::Overflow) |
| 107 | + } |
| 108 | + |
| 109 | + #[inline] |
| 110 | + fn safe_shr(&self, other: u32) -> Result<Self> { |
| 111 | + self.checked_shr(other).ok_or(ArithError::Overflow) |
| 112 | + } |
| 113 | + } |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +impl_safe_arith!(u8); |
| 118 | +impl_safe_arith!(u16); |
| 119 | +impl_safe_arith!(u32); |
| 120 | +impl_safe_arith!(u64); |
| 121 | +impl_safe_arith!(usize); |
| 122 | +impl_safe_arith!(i8); |
| 123 | +impl_safe_arith!(i16); |
| 124 | +impl_safe_arith!(i32); |
| 125 | +impl_safe_arith!(i64); |
| 126 | +impl_safe_arith!(isize); |
| 127 | + |
| 128 | +#[cfg(test)] |
| 129 | +mod test { |
| 130 | + use super::*; |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn basic() { |
| 134 | + // Test with u8 |
| 135 | + let x = 10u8; |
| 136 | + let y = 5u8; |
| 137 | + assert_eq!(x.safe_add(y), Ok(x + y)); |
| 138 | + assert_eq!(x.safe_sub(y), Ok(x - y)); |
| 139 | + assert_eq!(x.safe_mul(y), Ok(x * y)); |
| 140 | + assert_eq!(x.safe_div(y), Ok(x / y)); |
| 141 | + assert_eq!(x.safe_rem(y), Ok(x % y)); |
| 142 | + |
| 143 | + assert_eq!(x.safe_shl(2), Ok(x << 2)); |
| 144 | + assert_eq!(x.safe_shr(1), Ok(x >> 1)); |
| 145 | + |
| 146 | + // Test with u16 |
| 147 | + let x = 100u16; |
| 148 | + let y = 25u16; |
| 149 | + assert_eq!(x.safe_add(y), Ok(x + y)); |
| 150 | + assert_eq!(x.safe_sub(y), Ok(x - y)); |
| 151 | + assert_eq!(x.safe_mul(y), Ok(x * y)); |
| 152 | + assert_eq!(x.safe_div(y), Ok(x / y)); |
| 153 | + assert_eq!(x.safe_rem(y), Ok(x % y)); |
| 154 | + |
| 155 | + assert_eq!(x.safe_shl(3), Ok(x << 3)); |
| 156 | + assert_eq!(x.safe_shr(2), Ok(x >> 2)); |
| 157 | + } |
| 158 | + |
| 159 | + #[test] |
| 160 | + fn mutate() { |
| 161 | + // Test with edge case values |
| 162 | + let mut x = 1u8; |
| 163 | + x.safe_add_assign(254).unwrap(); |
| 164 | + assert_eq!(x, 255); |
| 165 | + x.safe_sub_assign(255).unwrap(); |
| 166 | + assert_eq!(x, 0); |
| 167 | + x.safe_add_assign(1).unwrap(); |
| 168 | + x.safe_shl_assign(7).unwrap(); |
| 169 | + assert_eq!(x, 128); |
| 170 | + x.safe_shr_assign(7).unwrap(); |
| 171 | + assert_eq!(x, 1); |
| 172 | + |
| 173 | + // Test with larger integer types |
| 174 | + let mut y = 100u16; |
| 175 | + y.safe_mul_assign(2).unwrap(); |
| 176 | + assert_eq!(y, 200); |
| 177 | + y.safe_div_assign(4).unwrap(); |
| 178 | + assert_eq!(y, 50); |
| 179 | + y.safe_add_assign(1000).unwrap(); |
| 180 | + assert_eq!(y, 1050); |
| 181 | + } |
| 182 | + |
| 183 | + #[test] |
| 184 | + fn errors() { |
| 185 | + // Overflow and underflow for u32 |
| 186 | + assert!(u32::MAX.safe_add(1).is_err()); |
| 187 | + assert!(u32::MIN.safe_sub(1).is_err()); |
| 188 | + assert!(u32::MAX.safe_mul(2).is_err()); |
| 189 | + |
| 190 | + // Division by zero |
| 191 | + assert!(10u32.safe_div(0).is_err()); |
| 192 | + assert!(10u32.safe_rem(0).is_err()); |
| 193 | + |
| 194 | + // Shift overflow |
| 195 | + assert!(u32::MAX.safe_shl(33).is_err()); |
| 196 | + assert!(u32::MAX.safe_shr(33).is_err()); |
| 197 | + |
| 198 | + // Edge cases for smaller types |
| 199 | + assert!(u8::MAX.safe_add(1).is_err()); |
| 200 | + assert!(u8::MIN.safe_sub(1).is_err()); |
| 201 | + assert!(u8::MAX.safe_mul(2).is_err()); |
| 202 | + |
| 203 | + // Shifting too far |
| 204 | + assert!(u8::MAX.safe_shl(9).is_err()); |
| 205 | + assert!(u8::MAX.safe_shr(9).is_err()); |
| 206 | + } |
| 207 | +} |
0 commit comments