@@ -335,22 +335,22 @@ impl Uint256 {
335
335
Self ( self . 0 . saturating_pow ( exp) )
336
336
}
337
337
338
- /// This is the same as [`Uint256::add`] but const .
338
+ /// Strict integer addition. Computes `self + rhs`, panicking if overflow occurred .
339
339
///
340
- /// Panics on overflow .
340
+ /// This is the same as [`Uint256::add`] but const .
341
341
#[ must_use = "this returns the result of the operation, without modifying the original" ]
342
- pub const fn panicking_add ( self , other : Self ) -> Self {
343
- match self . 0 . checked_add ( other . 0 ) {
342
+ pub const fn strict_add ( self , rhs : Self ) -> Self {
343
+ match self . 0 . checked_add ( rhs . 0 ) {
344
344
None => panic ! ( "attempt to add with overflow" ) ,
345
345
Some ( sum) => Self ( sum) ,
346
346
}
347
347
}
348
348
349
- /// This is the same as [`Uint256::sub`] but const .
349
+ /// Strict integer subtraction. Computes `self - rhs`, panicking if overflow occurred .
350
350
///
351
- /// Panics on overflow .
351
+ /// This is the same as [`Uint256::sub`] but const .
352
352
#[ must_use = "this returns the result of the operation, without modifying the original" ]
353
- pub const fn panicking_sub ( self , other : Self ) -> Self {
353
+ pub const fn strict_sub ( self , other : Self ) -> Self {
354
354
match self . 0 . checked_sub ( other. 0 ) {
355
355
None => panic ! ( "attempt to subtract with overflow" ) ,
356
356
Some ( diff) => Self ( diff) ,
@@ -464,7 +464,7 @@ impl Add<Uint256> for Uint256 {
464
464
type Output = Self ;
465
465
466
466
fn add ( self , rhs : Self ) -> Self {
467
- self . panicking_add ( rhs)
467
+ self . strict_add ( rhs)
468
468
}
469
469
}
470
470
forward_ref_binop ! ( impl Add , add for Uint256 , Uint256 ) ;
@@ -473,7 +473,7 @@ impl Sub<Uint256> for Uint256 {
473
473
type Output = Self ;
474
474
475
475
fn sub ( self , rhs : Self ) -> Self {
476
- self . panicking_sub ( rhs)
476
+ self . strict_sub ( rhs)
477
477
}
478
478
}
479
479
forward_ref_binop ! ( impl Sub , sub for Uint256 , Uint256 ) ;
@@ -1740,18 +1740,34 @@ mod tests {
1740
1740
}
1741
1741
1742
1742
#[ test]
1743
- fn uint256_panicking_sub_works ( ) {
1743
+ fn uint256_strict_add_works ( ) {
1744
+ let a = Uint256 :: from ( 5u32 ) ;
1745
+ let b = Uint256 :: from ( 3u32 ) ;
1746
+ assert_eq ! ( a. strict_add( b) , Uint256 :: from( 8u32 ) ) ;
1747
+ assert_eq ! ( b. strict_add( a) , Uint256 :: from( 8u32 ) ) ;
1748
+ }
1749
+
1750
+ #[ test]
1751
+ #[ should_panic( expected = "attempt to add with overflow" ) ]
1752
+ fn uint256_strict_add_panics_on_overflow ( ) {
1753
+ let a = Uint256 :: MAX ;
1754
+ let b = Uint256 :: ONE ;
1755
+ let _ = a. strict_add ( b) ;
1756
+ }
1757
+
1758
+ #[ test]
1759
+ fn uint256_strict_sub_works ( ) {
1744
1760
let a = Uint256 :: from ( 5u32 ) ;
1745
1761
let b = Uint256 :: from ( 3u32 ) ;
1746
- assert_eq ! ( a. panicking_sub ( b) , Uint256 :: from( 2u32 ) ) ;
1762
+ assert_eq ! ( a. strict_sub ( b) , Uint256 :: from( 2u32 ) ) ;
1747
1763
}
1748
1764
1749
1765
#[ test]
1750
1766
#[ should_panic( expected = "attempt to subtract with overflow" ) ]
1751
- fn uint256_panicking_sub_panics_on_overflow ( ) {
1767
+ fn uint256_strict_sub_panics_on_overflow ( ) {
1752
1768
let a = Uint256 :: ZERO ;
1753
1769
let b = Uint256 :: ONE ;
1754
- let _diff = a. panicking_sub ( b) ;
1770
+ let _ = a. strict_sub ( b) ;
1755
1771
}
1756
1772
1757
1773
#[ test]
0 commit comments