11use std:: {
2- ops :: { Deref , DerefMut } ,
3- str :: FromStr ,
2+ iter :: Sum ,
3+ ops :: { Add , Deref } ,
44} ;
55
66use k8s_openapi:: apimachinery:: pkg:: api:: resource:: Quantity as K8sQuantity ;
77
8- use crate :: quantity:: {
9- macros:: { forward_from_impls, forward_op_impls} ,
10- BinaryByteMultiple , ParseQuantityError , Quantity , Suffix ,
11- } ;
12-
13- pub trait JavaHeap {
14- // TODO (@Techassi): Add proper error type
15- /// Formats the [`MemoryQuantity`] so that it can be used as a Java heap value.
16- ///
17- /// This function can fail, because the [`Quantity`] has to be scaled down to at most
18- fn to_java_heap_string ( & self ) -> Result < String , String > ;
19- }
8+ use crate :: quantity:: { macros:: forward_quantity_impls, BinaryMultiple , Quantity , Suffix } ;
209
2110#[ derive( Clone , Copy , Debug , PartialEq , PartialOrd ) ]
2211pub struct MemoryQuantity ( Quantity ) ;
@@ -29,53 +18,57 @@ impl Deref for MemoryQuantity {
2918 }
3019}
3120
32- impl DerefMut for MemoryQuantity {
33- fn deref_mut ( & mut self ) -> & mut Self :: Target {
34- & mut self . 0
35- }
36- }
37-
38- impl FromStr for MemoryQuantity {
39- type Err = ParseQuantityError ;
40-
41- fn from_str ( input : & str ) -> Result < Self , Self :: Err > {
42- let quantity = Quantity :: from_str ( input) ?;
43- Ok ( Self ( quantity) )
44- }
45- }
46-
4721impl From < MemoryQuantity > for K8sQuantity {
4822 fn from ( value : MemoryQuantity ) -> Self {
4923 K8sQuantity ( value. to_string ( ) )
5024 }
5125}
5226
53- forward_from_impls ! ( Quantity , K8sQuantity , MemoryQuantity ) ;
54- forward_op_impls ! (
55- MemoryQuantity ( Quantity {
56- value : 0.0 ,
57- // TODO (@Techassi): This needs to be talked about. The previous implementation used Kibi
58- // here. Code which relies on that fact (for later scaling) will thus break.
59- suffix : None ,
60- } ) ,
61- MemoryQuantity ,
62- usize ,
63- f32 ,
64- f64
65- ) ;
27+ impl Sum for MemoryQuantity {
28+ fn sum < I : Iterator < Item = Self > > ( iter : I ) -> Self {
29+ iter . fold (
30+ MemoryQuantity ( Quantity {
31+ value : 0.0 ,
32+ suffix : Suffix :: BinaryMultiple ( BinaryMultiple :: Kibi ) ,
33+ } ) ,
34+ MemoryQuantity :: add ,
35+ )
36+ }
37+ }
38+
39+ forward_quantity_impls ! ( MemoryQuantity , K8sQuantity , usize , f32 , f64 ) ;
6640
6741impl MemoryQuantity {
6842 pub const fn from_gibi ( value : f64 ) -> Self {
6943 MemoryQuantity ( Quantity {
70- suffix : Some ( Suffix :: BinaryByteMultiple ( BinaryByteMultiple :: Gibi ) ) ,
44+ suffix : Suffix :: BinaryMultiple ( BinaryMultiple :: Gibi ) ,
7145 value,
7246 } )
7347 }
7448
7549 pub const fn from_mebi ( value : f64 ) -> Self {
7650 MemoryQuantity ( Quantity {
77- suffix : Some ( Suffix :: BinaryByteMultiple ( BinaryByteMultiple :: Mebi ) ) ,
51+ suffix : Suffix :: BinaryMultiple ( BinaryMultiple :: Mebi ) ,
7852 value,
7953 } )
8054 }
55+
56+ pub fn scale_to ( self , suffix : Suffix ) -> Self {
57+ Self ( self . 0 . scale_to ( suffix) )
58+ }
59+
60+ pub fn ceil ( self ) -> Self {
61+ Self ( Quantity {
62+ value : self . value . ceil ( ) ,
63+ suffix : self . suffix ,
64+ } )
65+ }
66+ }
67+
68+ pub trait JavaHeap {
69+ // TODO (@Techassi): Add proper error type
70+ /// Formats the [`MemoryQuantity`] so that it can be used as a Java heap value.
71+ ///
72+ /// This function can fail, because the [`Quantity`] has to be scaled down to at most
73+ fn to_java_heap_string ( & self ) -> Result < String , String > ;
8174}
0 commit comments