@@ -18,7 +18,7 @@ impl std::str::FromStr for ByteSize {
1818 . skip_while ( |c| c. is_whitespace ( ) || c. is_digit ( 10 ) || c == & '.' )
1919 . collect ( ) ;
2020 match suffix. parse :: < Unit > ( ) {
21- Ok ( u) => Ok ( Self ( ( v * u. factor ( ) as f64 ) as u64 ) ) ,
21+ Ok ( u) => Ok ( Self ( ( v * u) as u64 ) ) ,
2222 Err ( error) => Err ( format ! (
2323 "couldn't parse {:?} into a known SI unit, {}" ,
2424 suffix, error
@@ -69,6 +69,75 @@ impl Unit {
6969 }
7070}
7171
72+ mod impl_ops {
73+ use super :: Unit ;
74+ use std:: ops;
75+
76+ impl ops:: Add < u64 > for Unit {
77+ type Output = u64 ;
78+
79+ fn add ( self , other : u64 ) -> Self :: Output {
80+ self . factor ( ) + other
81+ }
82+ }
83+
84+ impl ops:: Add < Unit > for u64 {
85+ type Output = u64 ;
86+
87+ fn add ( self , other : Unit ) -> Self :: Output {
88+ self + other. factor ( )
89+ }
90+ }
91+
92+ impl ops:: Mul < u64 > for Unit {
93+ type Output = u64 ;
94+
95+ fn mul ( self , other : u64 ) -> Self :: Output {
96+ self . factor ( ) * other
97+ }
98+ }
99+
100+ impl ops:: Mul < Unit > for u64 {
101+ type Output = u64 ;
102+
103+ fn mul ( self , other : Unit ) -> Self :: Output {
104+ self * other. factor ( )
105+ }
106+ }
107+
108+ impl ops:: Add < f64 > for Unit {
109+ type Output = f64 ;
110+
111+ fn add ( self , other : f64 ) -> Self :: Output {
112+ self . factor ( ) as f64 + other
113+ }
114+ }
115+
116+ impl ops:: Add < Unit > for f64 {
117+ type Output = f64 ;
118+
119+ fn add ( self , other : Unit ) -> Self :: Output {
120+ other. factor ( ) as f64 + self
121+ }
122+ }
123+
124+ impl ops:: Mul < f64 > for Unit {
125+ type Output = f64 ;
126+
127+ fn mul ( self , other : f64 ) -> Self :: Output {
128+ self . factor ( ) as f64 * other
129+ }
130+ }
131+
132+ impl ops:: Mul < Unit > for f64 {
133+ type Output = f64 ;
134+
135+ fn mul ( self , other : Unit ) -> Self :: Output {
136+ other. factor ( ) as f64 * self
137+ }
138+ }
139+ }
140+
72141impl std:: str:: FromStr for Unit {
73142 type Err = String ;
74143
@@ -106,20 +175,20 @@ mod tests {
106175 assert_eq ! ( "0" . parse:: <ByteSize >( ) . unwrap( ) . 0 , 0 ) ;
107176 assert_eq ! ( parse( "0" ) , 0 ) ;
108177 assert_eq ! ( parse( "500" ) , 500 ) ;
109- assert_eq ! ( parse( "1K" ) , 1 * crate :: KB ) ;
110- assert_eq ! ( parse( "1Ki" ) , 1 * crate :: KIB ) ;
111- assert_eq ! ( parse( "1.5Ki" ) , ( 1.5 * crate :: KIB as f64 ) as u64 ) ;
112- assert_eq ! ( parse( "1KiB" ) , 1 * crate :: KIB ) ;
113- assert_eq ! ( parse( "1.5KiB" ) , ( 1.5 * crate :: KIB as f64 ) as u64 ) ;
114- assert_eq ! ( parse( "3 MB" ) , 3 * crate :: MB ) ;
115- assert_eq ! ( parse( "4 MiB" ) , 4 * crate :: MIB ) ;
116- assert_eq ! ( parse( "6 GB" ) , 6 * crate :: GB ) ;
117- assert_eq ! ( parse( "4 GiB" ) , 4 * crate :: GIB ) ;
118- assert_eq ! ( parse( "88TB" ) , 88 * crate :: TB ) ;
119- assert_eq ! ( parse( "521TiB" ) , 521 * crate :: TIB ) ;
120- assert_eq ! ( parse( "8 PB" ) , 8 * crate :: PB ) ;
121- assert_eq ! ( parse( "8P" ) , 8 * crate :: PB ) ;
122- assert_eq ! ( parse( "12 PiB" ) , 12 * crate :: PIB ) ;
178+ assert_eq ! ( parse( "1K" ) , Unit :: KiloByte * 1 ) ;
179+ assert_eq ! ( parse( "1Ki" ) , Unit :: KibiByte * 1 ) ;
180+ assert_eq ! ( parse( "1.5Ki" ) , ( 1.5 * Unit :: KibiByte ) as u64 ) ;
181+ assert_eq ! ( parse( "1KiB" ) , 1 * Unit :: KibiByte ) ;
182+ assert_eq ! ( parse( "1.5KiB" ) , ( 1.5 * Unit :: KibiByte ) as u64 ) ;
183+ assert_eq ! ( parse( "3 MB" ) , Unit :: MegaByte * 3 ) ;
184+ assert_eq ! ( parse( "4 MiB" ) , Unit :: MebiByte * 4 ) ;
185+ assert_eq ! ( parse( "6 GB" ) , 6 * Unit :: GigaByte ) ;
186+ assert_eq ! ( parse( "4 GiB" ) , 4 * Unit :: GibiByte ) ;
187+ assert_eq ! ( parse( "88TB" ) , 88 * Unit :: TeraByte ) ;
188+ assert_eq ! ( parse( "521TiB" ) , 521 * Unit :: TebiByte ) ;
189+ assert_eq ! ( parse( "8 PB" ) , 8 * Unit :: PetaByte ) ;
190+ assert_eq ! ( parse( "8P" ) , 8 * Unit :: PetaByte ) ;
191+ assert_eq ! ( parse( "12 PiB" ) , 12 * Unit :: PebiByte ) ;
123192 }
124193
125194 #[ test]
@@ -140,10 +209,10 @@ mod tests {
140209 s. parse :: < ByteSize > ( ) . unwrap ( ) . 0
141210 }
142211
143- assert_eq ! ( parse( & format!( "{}" , parse( "128GB" ) ) ) , 128 * crate :: GB ) ;
212+ assert_eq ! ( parse( & format!( "{}" , parse( "128GB" ) ) ) , 128 * Unit :: GigaByte ) ;
144213 assert_eq ! (
145214 parse( & crate :: to_string( parse( "128.000 GiB" ) , true ) ) ,
146- 128 * crate :: GIB
215+ 128 * Unit :: GibiByte
147216 ) ;
148217 }
149218}
0 commit comments