@@ -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 ) ;
@@ -1744,18 +1744,34 @@ mod tests {
1744
1744
}
1745
1745
1746
1746
#[ test]
1747
- fn uint256_panicking_sub_works ( ) {
1747
+ fn uint256_strict_add_works ( ) {
1748
+ let a = Uint256 :: from ( 5u32 ) ;
1749
+ let b = Uint256 :: from ( 3u32 ) ;
1750
+ assert_eq ! ( a. strict_add( b) , Uint256 :: from( 8u32 ) ) ;
1751
+ assert_eq ! ( b. strict_add( a) , Uint256 :: from( 8u32 ) ) ;
1752
+ }
1753
+
1754
+ #[ test]
1755
+ #[ should_panic( expected = "attempt to add with overflow" ) ]
1756
+ fn uint256_strict_add_panics_on_overflow ( ) {
1757
+ let a = Uint256 :: MAX ;
1758
+ let b = Uint256 :: ONE ;
1759
+ let _ = a. strict_add ( b) ;
1760
+ }
1761
+
1762
+ #[ test]
1763
+ fn uint256_strict_sub_works ( ) {
1748
1764
let a = Uint256 :: from ( 5u32 ) ;
1749
1765
let b = Uint256 :: from ( 3u32 ) ;
1750
- assert_eq ! ( a. panicking_sub ( b) , Uint256 :: from( 2u32 ) ) ;
1766
+ assert_eq ! ( a. strict_sub ( b) , Uint256 :: from( 2u32 ) ) ;
1751
1767
}
1752
1768
1753
1769
#[ test]
1754
1770
#[ should_panic( expected = "attempt to subtract with overflow" ) ]
1755
- fn uint256_panicking_sub_panics_on_overflow ( ) {
1771
+ fn uint256_strict_sub_panics_on_overflow ( ) {
1756
1772
let a = Uint256 :: ZERO ;
1757
1773
let b = Uint256 :: ONE ;
1758
- let _diff = a. panicking_sub ( b) ;
1774
+ let _ = a. strict_sub ( b) ;
1759
1775
}
1760
1776
1761
1777
#[ test]
0 commit comments