diff --git a/packages/std/src/math/int128.rs b/packages/std/src/math/int128.rs index bc5cd241fc..a3b23ce940 100644 --- a/packages/std/src/math/int128.rs +++ b/packages/std/src/math/int128.rs @@ -97,8 +97,11 @@ impl Int128 { } #[must_use = "this returns the result of the operation, without modifying the original"] - pub fn pow(self, exp: u32) -> Self { - Self(self.0.pow(exp)) + pub const fn pow(self, exp: u32) -> Self { + match self.0.checked_pow(exp) { + Some(val) => Self(val), + None => panic!("attempt to exponentiate with overflow"), + } } /// Returns `self * numerator / denominator`. @@ -262,13 +265,26 @@ impl Int128 { #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn abs(self) -> Self { - Self(self.0.abs()) + match self.0.checked_abs() { + Some(val) => Self(val), + None => panic!("attempt to calculate absolute value with overflow"), + } } #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn unsigned_abs(self) -> Uint128 { Uint128(self.0.unsigned_abs()) } + + /// Strict negation. Computes -self, panicking if self == MIN. + /// + /// This is the same as [`Int128::neg`] but const. + pub const fn strict_neg(self) -> Self { + match self.0.checked_neg() { + Some(val) => Self(val), + None => panic!("attempt to negate with overflow"), + } + } } impl NumConsts for Int128 { @@ -444,7 +460,7 @@ impl Neg for Int128 { type Output = Self; fn neg(self) -> Self::Output { - Self(-self.0) + self.strict_neg() } } @@ -1267,7 +1283,7 @@ mod tests { } #[test] - #[should_panic = "attempt to negate with overflow"] + #[should_panic = "attempt to calculate absolute value with overflow"] fn int128_abs_min_panics() { _ = Int128::MIN.abs(); } diff --git a/packages/std/src/math/int256.rs b/packages/std/src/math/int256.rs index 70d17417d7..c0eb675d72 100644 --- a/packages/std/src/math/int256.rs +++ b/packages/std/src/math/int256.rs @@ -152,8 +152,11 @@ impl Int256 { } #[must_use = "this returns the result of the operation, without modifying the original"] - pub fn pow(self, exp: u32) -> Self { - Self(self.0.pow(exp)) + pub const fn pow(self, exp: u32) -> Self { + match self.0.checked_pow(exp) { + Some(val) => Self(val), + None => panic!("attempt to exponentiate with overflow"), + } } /// Returns `self * numerator / denominator`. @@ -320,13 +323,26 @@ impl Int256 { #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn abs(self) -> Self { - Self(self.0.abs()) + match self.0.checked_abs() { + Some(val) => Self(val), + None => panic!("attempt to calculate absolute value with overflow"), + } } #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn unsigned_abs(self) -> Uint256 { Uint256(self.0.unsigned_abs()) } + + /// Strict negation. Computes -self, panicking if self == MIN. + /// + /// This is the same as [`Int256::neg`] but const. + pub const fn strict_neg(self) -> Self { + match self.0.checked_neg() { + Some(val) => Self(val), + None => panic!("attempt to negate with overflow"), + } + } } impl NumConsts for Int256 { @@ -519,7 +535,7 @@ impl Neg for Int256 { type Output = Self; fn neg(self) -> Self::Output { - Self(-self.0) + self.strict_neg() } } @@ -1376,7 +1392,7 @@ mod tests { } #[test] - #[should_panic = "attempt to negate with overflow"] + #[should_panic = "attempt to calculate absolute value with overflow"] fn int256_abs_min_panics() { _ = Int256::MIN.abs(); } diff --git a/packages/std/src/math/int512.rs b/packages/std/src/math/int512.rs index 86b4a93b3a..b26441943e 100644 --- a/packages/std/src/math/int512.rs +++ b/packages/std/src/math/int512.rs @@ -179,8 +179,11 @@ impl Int512 { } #[must_use = "this returns the result of the operation, without modifying the original"] - pub fn pow(self, exp: u32) -> Self { - Self(self.0.pow(exp)) + pub const fn pow(self, exp: u32) -> Self { + match self.0.checked_pow(exp) { + Some(val) => Self(val), + None => panic!("attempt to exponentiate with overflow"), + } } pub fn checked_add(self, other: Self) -> Result { @@ -305,13 +308,26 @@ impl Int512 { #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn abs(self) -> Self { - Self(self.0.abs()) + match self.0.checked_abs() { + Some(val) => Self(val), + None => panic!("attempt to calculate absolute value with overflow"), + } } #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn unsigned_abs(self) -> Uint512 { Uint512(self.0.unsigned_abs()) } + + /// Strict negation. Computes -self, panicking if self == MIN. + /// + /// This is the same as [`Int512::neg`] but const. + pub const fn strict_neg(self) -> Self { + match self.0.checked_neg() { + Some(val) => Self(val), + None => panic!("attempt to negate with overflow"), + } + } } impl NumConsts for Int512 { @@ -516,7 +532,7 @@ impl Neg for Int512 { type Output = Self; fn neg(self) -> Self::Output { - Self(-self.0) + self.strict_neg() } } @@ -1349,7 +1365,7 @@ mod tests { } #[test] - #[should_panic = "attempt to negate with overflow"] + #[should_panic = "attempt to calculate absolute value with overflow"] fn int512_abs_min_panics() { _ = Int512::MIN.abs(); } diff --git a/packages/std/src/math/int64.rs b/packages/std/src/math/int64.rs index 86a9bc4778..4115f22c80 100644 --- a/packages/std/src/math/int64.rs +++ b/packages/std/src/math/int64.rs @@ -97,8 +97,11 @@ impl Int64 { } #[must_use = "this returns the result of the operation, without modifying the original"] - pub fn pow(self, exp: u32) -> Self { - Self(self.0.pow(exp)) + pub const fn pow(self, exp: u32) -> Self { + match self.0.checked_pow(exp) { + Some(val) => Self(val), + None => panic!("attempt to exponentiate with overflow"), + } } /// Returns `self * numerator / denominator`. @@ -262,13 +265,26 @@ impl Int64 { #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn abs(self) -> Self { - Self(self.0.abs()) + match self.0.checked_abs() { + Some(val) => Self(val), + None => panic!("attempt to calculate absolute value with overflow"), + } } #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn unsigned_abs(self) -> Uint64 { Uint64(self.0.unsigned_abs()) } + + /// Strict negation. Computes -self, panicking if self == MIN. + /// + /// This is the same as [`Int64::neg`] but const. + pub const fn strict_neg(self) -> Self { + match self.0.checked_neg() { + Some(val) => Self(val), + None => panic!("attempt to negate with overflow"), + } + } } impl NumConsts for Int64 { @@ -423,7 +439,7 @@ impl Neg for Int64 { type Output = Self; fn neg(self) -> Self::Output { - Self(-self.0) + self.strict_neg() } } @@ -1205,7 +1221,7 @@ mod tests { } #[test] - #[should_panic = "attempt to negate with overflow"] + #[should_panic = "attempt to calculate absolute value with overflow"] fn int64_abs_min_panics() { _ = Int64::MIN.abs(); } diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs index a62fe85a31..4f3c30ac30 100644 --- a/packages/std/src/math/uint128.rs +++ b/packages/std/src/math/uint128.rs @@ -92,8 +92,11 @@ impl Uint128 { } #[must_use = "this returns the result of the operation, without modifying the original"] - pub fn pow(self, exp: u32) -> Self { - self.0.pow(exp).into() + pub const fn pow(self, exp: u32) -> Self { + match self.0.checked_pow(exp) { + Some(val) => Self(val), + None => panic!("attempt to exponentiate with overflow"), + } } /// Returns `self * numerator / denominator`. diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index 1140d5638e..148efbeda0 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -164,8 +164,11 @@ impl Uint256 { } #[must_use = "this returns the result of the operation, without modifying the original"] - pub fn pow(self, exp: u32) -> Self { - Self(self.0.pow(exp)) + pub const fn pow(self, exp: u32) -> Self { + match self.0.checked_pow(exp) { + Some(val) => Self(val), + None => panic!("attempt to exponentiate with overflow"), + } } /// Returns `self * numerator / denominator`. diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index 367b20cda8..c96fecffcb 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -188,8 +188,11 @@ impl Uint512 { } #[must_use = "this returns the result of the operation, without modifying the original"] - pub fn pow(self, exp: u32) -> Self { - Self(self.0.pow(exp)) + pub const fn pow(self, exp: u32) -> Self { + match self.0.checked_pow(exp) { + Some(val) => Self(val), + None => panic!("attempt to exponentiate with overflow"), + } } pub fn checked_add(self, other: Self) -> Result { diff --git a/packages/std/src/math/uint64.rs b/packages/std/src/math/uint64.rs index 53e1862bf1..7e635cfafc 100644 --- a/packages/std/src/math/uint64.rs +++ b/packages/std/src/math/uint64.rs @@ -86,8 +86,11 @@ impl Uint64 { } #[must_use = "this returns the result of the operation, without modifying the original"] - pub fn pow(self, exp: u32) -> Self { - self.0.pow(exp).into() + pub const fn pow(self, exp: u32) -> Self { + match self.0.checked_pow(exp) { + Some(val) => Self(val), + None => panic!("attempt to exponentiate with overflow"), + } } /// Returns `self * numerator / denominator`.