@@ -329,22 +329,22 @@ impl Uint256 {
329
329
Self ( self . 0 . saturating_pow ( exp) )
330
330
}
331
331
332
- /// This is the same as [`Uint256::add`] but const .
332
+ /// Strict integer addition. Computes `self + rhs`, panicking if overflow occurred .
333
333
///
334
- /// Panics on overflow .
334
+ /// This is the same as [`Uint256::add`] but const .
335
335
#[ must_use = "this returns the result of the operation, without modifying the original" ]
336
- pub const fn panicking_add ( self , other : Self ) -> Self {
337
- match self . 0 . checked_add ( other . 0 ) {
336
+ pub const fn strict_add ( self , rhs : Self ) -> Self {
337
+ match self . 0 . checked_add ( rhs . 0 ) {
338
338
None => panic ! ( "attempt to add with overflow" ) ,
339
339
Some ( sum) => Self ( sum) ,
340
340
}
341
341
}
342
342
343
- /// This is the same as [`Uint256::sub`] but const .
343
+ /// Strict integer subtraction. Computes `self - rhs`, panicking if overflow occurred .
344
344
///
345
- /// Panics on overflow .
345
+ /// This is the same as [`Uint256::sub`] but const .
346
346
#[ must_use = "this returns the result of the operation, without modifying the original" ]
347
- pub const fn panicking_sub ( self , other : Self ) -> Self {
347
+ pub const fn strict_sub ( self , other : Self ) -> Self {
348
348
match self . 0 . checked_sub ( other. 0 ) {
349
349
None => panic ! ( "attempt to subtract with overflow" ) ,
350
350
Some ( diff) => Self ( diff) ,
@@ -450,7 +450,7 @@ impl Add<Uint256> for Uint256 {
450
450
type Output = Self ;
451
451
452
452
fn add ( self , rhs : Self ) -> Self {
453
- self . panicking_add ( rhs)
453
+ self . strict_add ( rhs)
454
454
}
455
455
}
456
456
forward_ref_binop ! ( impl Add , add for Uint256 , Uint256 ) ;
@@ -459,7 +459,7 @@ impl Sub<Uint256> for Uint256 {
459
459
type Output = Self ;
460
460
461
461
fn sub ( self , rhs : Self ) -> Self {
462
- self . panicking_sub ( rhs)
462
+ self . strict_sub ( rhs)
463
463
}
464
464
}
465
465
forward_ref_binop ! ( impl Sub , sub for Uint256 , Uint256 ) ;
@@ -1705,18 +1705,34 @@ mod tests {
1705
1705
}
1706
1706
1707
1707
#[ test]
1708
- fn uint256_panicking_sub_works ( ) {
1708
+ fn uint256_strict_add_works ( ) {
1709
+ let a = Uint256 :: from ( 5u32 ) ;
1710
+ let b = Uint256 :: from ( 3u32 ) ;
1711
+ assert_eq ! ( a. strict_add( b) , Uint256 :: from( 8u32 ) ) ;
1712
+ assert_eq ! ( b. strict_add( a) , Uint256 :: from( 8u32 ) ) ;
1713
+ }
1714
+
1715
+ #[ test]
1716
+ #[ should_panic( expected = "attempt to add with overflow" ) ]
1717
+ fn uint256_strict_add_panics_on_overflow ( ) {
1718
+ let a = Uint256 :: MAX ;
1719
+ let b = Uint256 :: one ( ) ;
1720
+ let _ = a. strict_add ( b) ;
1721
+ }
1722
+
1723
+ #[ test]
1724
+ fn uint256_strict_sub_works ( ) {
1709
1725
let a = Uint256 :: from ( 5u32 ) ;
1710
1726
let b = Uint256 :: from ( 3u32 ) ;
1711
- assert_eq ! ( a. panicking_sub ( b) , Uint256 :: from( 2u32 ) ) ;
1727
+ assert_eq ! ( a. strict_sub ( b) , Uint256 :: from( 2u32 ) ) ;
1712
1728
}
1713
1729
1714
1730
#[ test]
1715
1731
#[ should_panic( expected = "attempt to subtract with overflow" ) ]
1716
- fn uint256_panicking_sub_panics_on_overflow ( ) {
1732
+ fn uint256_strict_sub_panics_on_overflow ( ) {
1717
1733
let a = Uint256 :: zero ( ) ;
1718
1734
let b = Uint256 :: one ( ) ;
1719
- let _diff = a. panicking_sub ( b) ;
1735
+ let _ = a. strict_sub ( b) ;
1720
1736
}
1721
1737
1722
1738
#[ test]
0 commit comments