@@ -115,24 +115,33 @@ impl TryFrom<&K8sQuantity> for Quantity {
115115}
116116
117117impl Quantity {
118+ // TODO (@Techassi): Discuss if this should consume or mutate in place. Consumption requires us
119+ // to add these function on specialized quantities (which then forward). If we mutate in place,
120+ // we could leverage the Deref impl instead.
121+
118122 /// Optionally scales up or down to the provided `suffix`.
119123 ///
120- /// It additionally returns `true` if the suffix was scaled, and `false` if it was not. This can
121- /// be the case if the suffixes already match, the quantity has no suffix, or if the value is 0.
122- pub fn scale_to ( self , suffix : Suffix ) -> ( Self , bool ) {
123- match ( self . value , & self . suffix ) {
124- ( _, None ) | ( 0.0 , _) => ( self , false ) ,
125- ( _, Some ( s) ) if * s == suffix => ( self , false ) ,
124+ /// It additionally returns `true` if the suffix was scaled, and `false` in the following cases:
125+ ///
126+ /// - the suffixes already match
127+ /// - the quantity has no suffix, in which case the suffix will be added without scaling
128+ /// - the value is 0
129+ pub fn scale_to ( & mut self , suffix : Suffix ) -> bool {
130+ match ( & mut self . value , & mut self . suffix ) {
131+ ( 0.0 , _) => false ,
132+ ( _, Some ( s) ) if * s == suffix => false ,
133+ ( _, None ) => {
134+ self . suffix = Some ( suffix) ;
135+ false
136+ }
126137 ( v, Some ( s) ) => {
127138 let factor = ( s. base ( ) as f64 ) . powf ( s. exponent ( ) )
128139 / ( suffix. base ( ) as f64 ) . powf ( suffix. exponent ( ) ) ;
129140
130- let quantity = Self {
131- value : v * factor,
132- suffix : Some ( suffix) ,
133- } ;
141+ * v = * v * factor;
142+ * s = suffix;
134143
135- ( quantity , true )
144+ false
136145 }
137146 }
138147 }
0 commit comments