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 dc1593d commit 607e7fc
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 20 deletions.
19 changes: 16 additions & 3 deletions packages/std/src/math/int128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,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"),
}
}

pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {
Expand Down Expand Up @@ -209,6 +212,16 @@ impl Int128 {
pub const fn abs_diff(self, other: Self) -> Uint128 {
Uint128(self.0.abs_diff(other.0))
}

/// 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 From<Uint64> for Int128 {
Expand Down Expand Up @@ -367,7 +380,7 @@ impl Neg for Int128 {
type Output = Self;

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

Expand Down
19 changes: 16 additions & 3 deletions packages/std/src/math/int256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,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"),
}
}

pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {
Expand Down Expand Up @@ -258,6 +261,16 @@ impl Int256 {
pub const fn abs_diff(self, other: Self) -> Uint256 {
Uint256(self.0.abs_diff(other.0))
}

/// 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 From<Uint128> for Int256 {
Expand Down Expand Up @@ -434,7 +447,7 @@ impl Neg for Int256 {
type Output = Self;

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

Expand Down
19 changes: 16 additions & 3 deletions packages/std/src/math/int512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,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 @@ -294,6 +297,16 @@ impl Int512 {
pub const fn abs_diff(self, other: Self) -> Uint512 {
Uint512(self.0.abs_diff(other.0))
}

/// 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 From<Uint256> for Int512 {
Expand Down Expand Up @@ -467,7 +480,7 @@ impl Neg for Int512 {
type Output = Self;

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

Expand Down
19 changes: 16 additions & 3 deletions packages/std/src/math/int64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,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"),
}
}

pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {
Expand Down Expand Up @@ -209,6 +212,16 @@ impl Int64 {
pub const fn abs_diff(self, other: Self) -> Uint64 {
Uint64(self.0.abs_diff(other.0))
}

/// 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 From<u32> for Int64 {
Expand Down Expand Up @@ -343,7 +356,7 @@ impl Neg for Int64 {
type Output = Self;

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

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 @@ -88,8 +88,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 @@ -158,8 +158,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 @@ -185,8 +185,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 @@ -81,8 +81,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 607e7fc

Please sign in to comment.