File tree 2 files changed +20
-7
lines changed
2 files changed +20
-7
lines changed Original file line number Diff line number Diff line change 1
1
use int:: LargeInt ;
2
2
use int:: Int ;
3
3
4
- trait Add : LargeInt {
5
- fn add ( self , other : Self ) -> Self {
4
+ trait UAdd : LargeInt {
5
+ fn uadd ( self , other : Self ) -> Self {
6
6
let ( low, carry) = self . low ( ) . overflowing_add ( other. low ( ) ) ;
7
7
let high = self . high ( ) . wrapping_add ( other. high ( ) ) ;
8
8
let carry = if carry { Self :: HighHalf :: ONE } else { Self :: HighHalf :: ZERO } ;
9
9
Self :: from_parts ( low, high. wrapping_add ( carry) )
10
10
}
11
11
}
12
12
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
+
13
23
impl Add for u128 { }
24
+ impl Add for i128 { }
14
25
15
- trait Addo : Int {
26
+ trait Addo : Add
27
+ where <Self as Int >:: UnsignedInt : UAdd
28
+ {
16
29
fn addo ( self , other : Self , overflow : & mut i32 ) -> Self {
17
30
* overflow = 0 ;
18
- let result = self . wrapping_add ( other) ;
31
+ let result = Add :: add ( self , other) ;
19
32
if other >= Self :: ZERO {
20
33
if result < self {
21
34
* overflow = 1 ;
Original file line number Diff line number Diff line change 1
1
use int:: LargeInt ;
2
- use int:: Int ;
3
2
4
3
trait Sub : LargeInt {
5
4
fn sub ( self , other : Self ) -> Self {
@@ -8,12 +7,13 @@ trait Sub: LargeInt {
8
7
}
9
8
}
10
9
10
+ impl Sub for i128 { }
11
11
impl Sub for u128 { }
12
12
13
- trait Subo : Int {
13
+ trait Subo : Sub {
14
14
fn subo ( self , other : Self , overflow : & mut i32 ) -> Self {
15
15
* overflow = 0 ;
16
- let result = self . wrapping_sub ( other) ;
16
+ let result = Sub :: sub ( self , other) ;
17
17
if other >= Self :: ZERO {
18
18
if result > self {
19
19
* overflow = 1 ;
You can’t perform that action at this time.
0 commit comments