@@ -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) ,
@@ -462,7 +462,7 @@ impl Add<Uint256> for Uint256 {
462
462
type Output = Self ;
463
463
464
464
fn add ( self , rhs : Self ) -> Self {
465
- self . panicking_add ( rhs)
465
+ self . strict_add ( rhs)
466
466
}
467
467
}
468
468
forward_ref_binop ! ( impl Add , add for Uint256 , Uint256 ) ;
@@ -471,7 +471,7 @@ impl Sub<Uint256> for Uint256 {
471
471
type Output = Self ;
472
472
473
473
fn sub ( self , rhs : Self ) -> Self {
474
- self . panicking_sub ( rhs)
474
+ self . strict_sub ( rhs)
475
475
}
476
476
}
477
477
forward_ref_binop ! ( impl Sub , sub for Uint256 , Uint256 ) ;
@@ -1738,18 +1738,34 @@ mod tests {
1738
1738
}
1739
1739
1740
1740
#[ test]
1741
- fn uint256_panicking_sub_works ( ) {
1741
+ fn uint256_strict_add_works ( ) {
1742
+ let a = Uint256 :: from ( 5u32 ) ;
1743
+ let b = Uint256 :: from ( 3u32 ) ;
1744
+ assert_eq ! ( a. strict_add( b) , Uint256 :: from( 8u32 ) ) ;
1745
+ assert_eq ! ( b. strict_add( a) , Uint256 :: from( 8u32 ) ) ;
1746
+ }
1747
+
1748
+ #[ test]
1749
+ #[ should_panic( expected = "attempt to add with overflow" ) ]
1750
+ fn uint256_strict_add_panics_on_overflow ( ) {
1751
+ let a = Uint256 :: MAX ;
1752
+ let b = Uint256 :: ONE ;
1753
+ let _ = a. strict_add ( b) ;
1754
+ }
1755
+
1756
+ #[ test]
1757
+ fn uint256_strict_sub_works ( ) {
1742
1758
let a = Uint256 :: from ( 5u32 ) ;
1743
1759
let b = Uint256 :: from ( 3u32 ) ;
1744
- assert_eq ! ( a. panicking_sub ( b) , Uint256 :: from( 2u32 ) ) ;
1760
+ assert_eq ! ( a. strict_sub ( b) , Uint256 :: from( 2u32 ) ) ;
1745
1761
}
1746
1762
1747
1763
#[ test]
1748
1764
#[ should_panic( expected = "attempt to subtract with overflow" ) ]
1749
- fn uint256_panicking_sub_panics_on_overflow ( ) {
1765
+ fn uint256_strict_sub_panics_on_overflow ( ) {
1750
1766
let a = Uint256 :: ZERO ;
1751
1767
let b = Uint256 :: ONE ;
1752
- let _diff = a. panicking_sub ( b) ;
1768
+ let _ = a. strict_sub ( b) ;
1753
1769
}
1754
1770
1755
1771
#[ test]
0 commit comments