4141//! assert_eq!(ByteSize::gb(996), minus);
4242//! ```
4343
44- use std:: {
45- fmt,
46- ops:: { Add , AddAssign , Mul , MulAssign , Sub , SubAssign } ,
47- } ;
44+ #![ cfg_attr( not( feature = "std" ) , no_std) ]
45+
46+ extern crate alloc;
47+
48+ use alloc:: string:: ToString as _;
49+ use core:: { fmt, ops} ;
4850
4951#[ cfg( feature = "arbitrary" ) ]
5052mod arbitrary;
@@ -56,7 +58,7 @@ mod serde;
5658pub use crate :: display:: Display ;
5759use crate :: display:: Format ;
5860
59- /// Number of bytes in 1 kilobyte
61+ /// Number of bytes in 1 kilobyte.
6062pub const KB : u64 = 1_000 ;
6163/// Number of bytes in 1 megabyte.
6264pub const MB : u64 = 1_000_000 ;
@@ -251,15 +253,15 @@ impl fmt::Debug for ByteSize {
251253
252254macro_rules! commutative_op {
253255 ( $t: ty) => {
254- impl Add <ByteSize > for $t {
256+ impl ops :: Add <ByteSize > for $t {
255257 type Output = ByteSize ;
256258 #[ inline( always) ]
257259 fn add( self , rhs: ByteSize ) -> ByteSize {
258260 ByteSize ( rhs. 0 + ( self as u64 ) )
259261 }
260262 }
261263
262- impl Mul <ByteSize > for $t {
264+ impl ops :: Mul <ByteSize > for $t {
263265 type Output = ByteSize ;
264266 #[ inline( always) ]
265267 fn mul( self , rhs: ByteSize ) -> ByteSize {
@@ -274,7 +276,7 @@ commutative_op!(u32);
274276commutative_op ! ( u16 ) ;
275277commutative_op ! ( u8 ) ;
276278
277- impl Add < ByteSize > for ByteSize {
279+ impl ops :: Add < ByteSize > for ByteSize {
278280 type Output = ByteSize ;
279281
280282 #[ inline( always) ]
@@ -283,14 +285,14 @@ impl Add<ByteSize> for ByteSize {
283285 }
284286}
285287
286- impl AddAssign < ByteSize > for ByteSize {
288+ impl ops :: AddAssign < ByteSize > for ByteSize {
287289 #[ inline( always) ]
288290 fn add_assign ( & mut self , rhs : ByteSize ) {
289291 self . 0 += rhs. 0
290292 }
291293}
292294
293- impl < T > Add < T > for ByteSize
295+ impl < T > ops :: Add < T > for ByteSize
294296where
295297 T : Into < u64 > ,
296298{
@@ -301,7 +303,7 @@ where
301303 }
302304}
303305
304- impl < T > AddAssign < T > for ByteSize
306+ impl < T > ops :: AddAssign < T > for ByteSize
305307where
306308 T : Into < u64 > ,
307309{
@@ -311,7 +313,7 @@ where
311313 }
312314}
313315
314- impl Sub < ByteSize > for ByteSize {
316+ impl ops :: Sub < ByteSize > for ByteSize {
315317 type Output = ByteSize ;
316318
317319 #[ inline( always) ]
@@ -320,14 +322,14 @@ impl Sub<ByteSize> for ByteSize {
320322 }
321323}
322324
323- impl SubAssign < ByteSize > for ByteSize {
325+ impl ops :: SubAssign < ByteSize > for ByteSize {
324326 #[ inline( always) ]
325327 fn sub_assign ( & mut self , rhs : ByteSize ) {
326328 self . 0 -= rhs. 0
327329 }
328330}
329331
330- impl < T > Sub < T > for ByteSize
332+ impl < T > ops :: Sub < T > for ByteSize
331333where
332334 T : Into < u64 > ,
333335{
@@ -338,7 +340,7 @@ where
338340 }
339341}
340342
341- impl < T > SubAssign < T > for ByteSize
343+ impl < T > ops :: SubAssign < T > for ByteSize
342344where
343345 T : Into < u64 > ,
344346{
@@ -348,7 +350,7 @@ where
348350 }
349351}
350352
351- impl < T > Mul < T > for ByteSize
353+ impl < T > ops :: Mul < T > for ByteSize
352354where
353355 T : Into < u64 > ,
354356{
@@ -359,7 +361,7 @@ where
359361 }
360362}
361363
362- impl < T > MulAssign < T > for ByteSize
364+ impl < T > ops :: MulAssign < T > for ByteSize
363365where
364366 T : Into < u64 > ,
365367{
@@ -371,6 +373,8 @@ where
371373
372374#[ cfg( test) ]
373375mod property_tests {
376+ use alloc:: string:: { String , ToString as _} ;
377+
374378 use super :: * ;
375379
376380 impl quickcheck:: Arbitrary for ByteSize {
@@ -393,15 +397,21 @@ mod property_tests {
393397 size. to_string( ) . len( ) < 11
394398 }
395399
396- // // currently fails on input like "14.0 EiB"
397- // fn string_round_trip(size: ByteSize) -> bool {
398- // size.to_string().parse::<ByteSize>().unwrap() == size
399- // }
400+ fn string_round_trip( size: ByteSize ) -> bool {
401+ // currently fails on many inputs above the pebibyte level
402+ if size > ByteSize :: pib( 1 ) {
403+ return true ;
404+ }
405+
406+ size. to_string( ) . parse:: <ByteSize >( ) . unwrap( ) == size
407+ }
400408 }
401409}
402410
403411#[ cfg( test) ]
404412mod tests {
413+ use alloc:: format;
414+
405415 use super :: * ;
406416
407417 #[ test]
0 commit comments