File tree Expand file tree Collapse file tree 2 files changed +20
-7
lines changed
Expand file tree Collapse file tree 2 files changed +20
-7
lines changed Original file line number Diff line number Diff line change 11use int:: LargeInt ;
22use int:: Int ;
33
4- trait Add : LargeInt {
5- fn add ( self , other : Self ) -> Self {
4+ trait UAdd : LargeInt {
5+ fn uadd ( self , other : Self ) -> Self {
66 let ( low, carry) = self . low ( ) . overflowing_add ( other. low ( ) ) ;
77 let high = self . high ( ) . wrapping_add ( other. high ( ) ) ;
88 let carry = if carry { Self :: HighHalf :: ONE } else { Self :: HighHalf :: ZERO } ;
99 Self :: from_parts ( low, high. wrapping_add ( carry) )
1010 }
1111}
1212
13+ impl UAdd for u128 { }
14+
15+ trait Add : Int
16+ where <Self as Int >:: UnsignedInt : UAdd
17+ {
18+ fn add ( self , other : Self ) -> Self {
19+ Self :: from_unsigned ( self . unsigned ( ) . uadd ( other. unsigned ( ) ) )
20+ }
21+ }
22+
1323impl Add for u128 { }
24+ impl Add for i128 { }
1425
15- trait Addo : Int {
26+ trait Addo : Add
27+ where <Self as Int >:: UnsignedInt : UAdd
28+ {
1629 fn addo ( self , other : Self , overflow : & mut i32 ) -> Self {
1730 * overflow = 0 ;
18- let result = self . wrapping_add ( other) ;
31+ let result = Add :: add ( self , other) ;
1932 if other >= Self :: ZERO {
2033 if result < self {
2134 * overflow = 1 ;
Original file line number Diff line number Diff line change 11use int:: LargeInt ;
2- use int:: Int ;
32
43trait Sub : LargeInt {
54 fn sub ( self , other : Self ) -> Self {
@@ -8,12 +7,13 @@ trait Sub: LargeInt {
87 }
98}
109
10+ impl Sub for i128 { }
1111impl Sub for u128 { }
1212
13- trait Subo : Int {
13+ trait Subo : Sub {
1414 fn subo ( self , other : Self , overflow : & mut i32 ) -> Self {
1515 * overflow = 0 ;
16- let result = self . wrapping_sub ( other) ;
16+ let result = Sub :: sub ( self , other) ;
1717 if other >= Self :: ZERO {
1818 if result > self {
1919 * overflow = 1 ;
You can’t perform that action at this time.
0 commit comments