Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Apr 16, 2024
1 parent 9f88e7c commit eff79bc
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 28 deletions.
26 changes: 21 additions & 5 deletions packages/std/src/math/int128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -444,7 +460,7 @@ impl Neg for Int128 {
type Output = Self;

fn neg(self) -> Self::Output {
Self(-self.0)
self.strict_neg()
}
}

Expand Down Expand Up @@ -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();
}
Expand Down
26 changes: 21 additions & 5 deletions packages/std/src/math/int256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -519,7 +535,7 @@ impl Neg for Int256 {
type Output = Self;

fn neg(self) -> Self::Output {
Self(-self.0)
self.strict_neg()
}
}

Expand Down Expand Up @@ -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();
}
Expand Down
26 changes: 21 additions & 5 deletions packages/std/src/math/int512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, OverflowError> {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -516,7 +532,7 @@ impl Neg for Int512 {
type Output = Self;

fn neg(self) -> Self::Output {
Self(-self.0)
self.strict_neg()
}
}

Expand Down Expand Up @@ -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();
}
Expand Down
26 changes: 21 additions & 5 deletions packages/std/src/math/int64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -423,7 +439,7 @@ impl Neg for Int64 {
type Output = Self;

fn neg(self) -> Self::Output {
Self(-self.0)
self.strict_neg()
}
}

Expand Down Expand Up @@ -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();
}
Expand Down
7 changes: 5 additions & 2 deletions packages/std/src/math/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
7 changes: 5 additions & 2 deletions packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
7 changes: 5 additions & 2 deletions packages/std/src/math/uint512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, OverflowError> {
Expand Down
7 changes: 5 additions & 2 deletions packages/std/src/math/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down

0 comments on commit eff79bc

Please sign in to comment.