Skip to content

Commit 607e7fc

Browse files
committed
Fix
1 parent dc1593d commit 607e7fc

File tree

8 files changed

+84
-20
lines changed

8 files changed

+84
-20
lines changed

packages/std/src/math/int128.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ impl Int128 {
8686
}
8787

8888
#[must_use = "this returns the result of the operation, without modifying the original"]
89-
pub fn pow(self, exp: u32) -> Self {
90-
Self(self.0.pow(exp))
89+
pub const fn pow(self, exp: u32) -> Self {
90+
match self.0.checked_pow(exp) {
91+
Some(val) => Self(val),
92+
None => panic!("attempt to exponentiate with overflow"),
93+
}
9194
}
9295

9396
pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {
@@ -209,6 +212,16 @@ impl Int128 {
209212
pub const fn abs_diff(self, other: Self) -> Uint128 {
210213
Uint128(self.0.abs_diff(other.0))
211214
}
215+
216+
/// Strict negation. Computes -self, panicking if self == MIN.
217+
///
218+
/// This is the same as [`Int128::neg`] but const.
219+
pub const fn strict_neg(self) -> Self {
220+
match self.0.checked_neg() {
221+
Some(val) => Self(val),
222+
None => panic!("attempt to negate with overflow"),
223+
}
224+
}
212225
}
213226

214227
impl From<Uint64> for Int128 {
@@ -367,7 +380,7 @@ impl Neg for Int128 {
367380
type Output = Self;
368381

369382
fn neg(self) -> Self::Output {
370-
Self(-self.0)
383+
self.strict_neg()
371384
}
372385
}
373386

packages/std/src/math/int256.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,11 @@ impl Int256 {
135135
}
136136

137137
#[must_use = "this returns the result of the operation, without modifying the original"]
138-
pub fn pow(self, exp: u32) -> Self {
139-
Self(self.0.pow(exp))
138+
pub const fn pow(self, exp: u32) -> Self {
139+
match self.0.checked_pow(exp) {
140+
Some(val) => Self(val),
141+
None => panic!("attempt to exponentiate with overflow"),
142+
}
140143
}
141144

142145
pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {
@@ -258,6 +261,16 @@ impl Int256 {
258261
pub const fn abs_diff(self, other: Self) -> Uint256 {
259262
Uint256(self.0.abs_diff(other.0))
260263
}
264+
265+
/// Strict negation. Computes -self, panicking if self == MIN.
266+
///
267+
/// This is the same as [`Int256::neg`] but const.
268+
pub const fn strict_neg(self) -> Self {
269+
match self.0.checked_neg() {
270+
Some(val) => Self(val),
271+
None => panic!("attempt to negate with overflow"),
272+
}
273+
}
261274
}
262275

263276
impl From<Uint128> for Int256 {
@@ -434,7 +447,7 @@ impl Neg for Int256 {
434447
type Output = Self;
435448

436449
fn neg(self) -> Self::Output {
437-
Self(-self.0)
450+
self.strict_neg()
438451
}
439452
}
440453

packages/std/src/math/int512.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,11 @@ impl Int512 {
171171
}
172172

173173
#[must_use = "this returns the result of the operation, without modifying the original"]
174-
pub fn pow(self, exp: u32) -> Self {
175-
Self(self.0.pow(exp))
174+
pub const fn pow(self, exp: u32) -> Self {
175+
match self.0.checked_pow(exp) {
176+
Some(val) => Self(val),
177+
None => panic!("attempt to exponentiate with overflow"),
178+
}
176179
}
177180

178181
pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {
@@ -294,6 +297,16 @@ impl Int512 {
294297
pub const fn abs_diff(self, other: Self) -> Uint512 {
295298
Uint512(self.0.abs_diff(other.0))
296299
}
300+
301+
/// Strict negation. Computes -self, panicking if self == MIN.
302+
///
303+
/// This is the same as [`Int512::neg`] but const.
304+
pub const fn strict_neg(self) -> Self {
305+
match self.0.checked_neg() {
306+
Some(val) => Self(val),
307+
None => panic!("attempt to negate with overflow"),
308+
}
309+
}
297310
}
298311

299312
impl From<Uint256> for Int512 {
@@ -467,7 +480,7 @@ impl Neg for Int512 {
467480
type Output = Self;
468481

469482
fn neg(self) -> Self::Output {
470-
Self(-self.0)
483+
self.strict_neg()
471484
}
472485
}
473486

packages/std/src/math/int64.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ impl Int64 {
8686
}
8787

8888
#[must_use = "this returns the result of the operation, without modifying the original"]
89-
pub fn pow(self, exp: u32) -> Self {
90-
Self(self.0.pow(exp))
89+
pub const fn pow(self, exp: u32) -> Self {
90+
match self.0.checked_pow(exp) {
91+
Some(val) => Self(val),
92+
None => panic!("attempt to exponentiate with overflow"),
93+
}
9194
}
9295

9396
pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {
@@ -209,6 +212,16 @@ impl Int64 {
209212
pub const fn abs_diff(self, other: Self) -> Uint64 {
210213
Uint64(self.0.abs_diff(other.0))
211214
}
215+
216+
/// Strict negation. Computes -self, panicking if self == MIN.
217+
///
218+
/// This is the same as [`Int64::neg`] but const.
219+
pub const fn strict_neg(self) -> Self {
220+
match self.0.checked_neg() {
221+
Some(val) => Self(val),
222+
None => panic!("attempt to negate with overflow"),
223+
}
224+
}
212225
}
213226

214227
impl From<u32> for Int64 {
@@ -343,7 +356,7 @@ impl Neg for Int64 {
343356
type Output = Self;
344357

345358
fn neg(self) -> Self::Output {
346-
Self(-self.0)
359+
self.strict_neg()
347360
}
348361
}
349362

packages/std/src/math/uint128.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@ impl Uint128 {
8888
}
8989

9090
#[must_use = "this returns the result of the operation, without modifying the original"]
91-
pub fn pow(self, exp: u32) -> Self {
92-
self.0.pow(exp).into()
91+
pub const fn pow(self, exp: u32) -> Self {
92+
match self.0.checked_pow(exp) {
93+
Some(val) => Self(val),
94+
None => panic!("attempt to exponentiate with overflow"),
95+
}
9396
}
9497

9598
/// Returns `self * numerator / denominator`.

packages/std/src/math/uint256.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,11 @@ impl Uint256 {
158158
}
159159

160160
#[must_use = "this returns the result of the operation, without modifying the original"]
161-
pub fn pow(self, exp: u32) -> Self {
162-
Self(self.0.pow(exp))
161+
pub const fn pow(self, exp: u32) -> Self {
162+
match self.0.checked_pow(exp) {
163+
Some(val) => Self(val),
164+
None => panic!("attempt to exponentiate with overflow"),
165+
}
163166
}
164167

165168
/// Returns `self * numerator / denominator`.

packages/std/src/math/uint512.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,11 @@ impl Uint512 {
185185
}
186186

187187
#[must_use = "this returns the result of the operation, without modifying the original"]
188-
pub fn pow(self, exp: u32) -> Self {
189-
Self(self.0.pow(exp))
188+
pub const fn pow(self, exp: u32) -> Self {
189+
match self.0.checked_pow(exp) {
190+
Some(val) => Self(val),
191+
None => panic!("attempt to exponentiate with overflow"),
192+
}
190193
}
191194

192195
pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {

packages/std/src/math/uint64.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ impl Uint64 {
8181
}
8282

8383
#[must_use = "this returns the result of the operation, without modifying the original"]
84-
pub fn pow(self, exp: u32) -> Self {
85-
self.0.pow(exp).into()
84+
pub const fn pow(self, exp: u32) -> Self {
85+
match self.0.checked_pow(exp) {
86+
Some(val) => Self(val),
87+
None => panic!("attempt to exponentiate with overflow"),
88+
}
8689
}
8790

8891
/// Returns `self * numerator / denominator`.

0 commit comments

Comments
 (0)