@@ -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
263276impl 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
0 commit comments