File tree 3 files changed +27
-8
lines changed
crates/stackable-operator/src/quantity
3 files changed +27
-8
lines changed Original file line number Diff line number Diff line change @@ -142,6 +142,25 @@ impl Quantity {
142
142
}
143
143
}
144
144
}
145
+
146
+ /// Either sets the suffix of `self` to `rhs` or scales `rhs` if `self` has a value other than
147
+ /// zero.
148
+ ///
149
+ /// This function is currently used for the [`std::ops::Add`] and [`std::ops::Sub`]
150
+ /// implementations.
151
+ pub fn set_suffix_or_scale_rhs ( self , rhs : Self ) -> ( Self , Self ) {
152
+ if self . value == 0.0 {
153
+ (
154
+ Self {
155
+ suffix : rhs. suffix ,
156
+ ..self
157
+ } ,
158
+ rhs,
159
+ )
160
+ } else {
161
+ ( self , rhs. scale_to ( self . suffix ) )
162
+ }
163
+ }
145
164
}
146
165
147
166
#[ cfg( test) ]
Original file line number Diff line number Diff line change @@ -6,11 +6,11 @@ impl Add for Quantity {
6
6
type Output = Quantity ;
7
7
8
8
fn add ( self , rhs : Quantity ) -> Self :: Output {
9
- let rhs = rhs . scale_to ( self . suffix ) ;
9
+ let ( this , rhs) = self . set_suffix_or_scale_rhs ( rhs ) ;
10
10
11
11
Self {
12
- value : self . value + rhs. value ,
13
- .. self
12
+ value : this . value + rhs. value ,
13
+ suffix : this . suffix ,
14
14
}
15
15
}
16
16
}
@@ -25,11 +25,11 @@ impl Sub for Quantity {
25
25
type Output = Quantity ;
26
26
27
27
fn sub ( self , rhs : Quantity ) -> Self :: Output {
28
- let rhs = rhs . scale_to ( self . suffix ) ;
28
+ let ( this , rhs) = self . set_suffix_or_scale_rhs ( rhs ) ;
29
29
30
30
Self {
31
- value : self . value - rhs. value ,
32
- .. self
31
+ value : this . value - rhs. value ,
32
+ suffix : this . suffix ,
33
33
}
34
34
}
35
35
}
Original file line number Diff line number Diff line change @@ -58,12 +58,12 @@ impl Suffix {
58
58
59
59
pub fn scale_down ( self ) -> Option < Self > {
60
60
match self {
61
- Suffix :: DecimalMultiple ( s ) => todo ! ( ) ,
61
+ Suffix :: DecimalMultiple ( _s ) => todo ! ( ) ,
62
62
Suffix :: BinaryMultiple ( s) => match s. scale_down ( ) {
63
63
Some ( s) => Some ( Self :: BinaryMultiple ( s) ) ,
64
64
None => Some ( Self :: DecimalMultiple ( DecimalMultiple :: Milli ) ) ,
65
65
} ,
66
- Suffix :: DecimalExponent ( s ) => todo ! ( ) ,
66
+ Suffix :: DecimalExponent ( _s ) => todo ! ( ) ,
67
67
}
68
68
}
69
69
}
You can’t perform that action at this time.
0 commit comments