11## ByteSize
2+
23[ ![ Build Status] ( https://travis-ci.org/hyunsik/bytesize.svg?branch=master )] ( https://travis-ci.org/hyunsik/bytesize )
34[ ![ Crates.io Version] ( https://img.shields.io/crates/v/bytesize.svg )] ( https://crates.io/crates/bytesize )
45
5-
66ByteSize is a utility for human-readable byte count representation.
77
88[ API Documentation] ( https://docs.rs/bytesize/ )
@@ -16,80 +16,32 @@ Add this to your Cargo.toml:
1616bytesize = " 1.0.0"
1717```
1818
19- and this to your crate root:
20- ``` rust
21- extern crate bytesize;
22- ```
19+ ## Examples
2320
24- ## Example
2521### Human readable representations (SI unit and Binary unit)
22+
2623``` rust
27- #[allow(dead_code)]
28- fn assert_display (expected : & str , b : ByteSize ) {
29- assert_eq! (expected , format! (" {}" , b ));
30- }
31-
32- #[test]
33- fn test_display () {
34- assert_display (" 215 B" , ByteSize (215 ));
35- assert_display (" 215 B" , ByteSize :: b (215 ));
36- assert_display (" 1.0 KB" , ByteSize :: kb (1 ));
37- assert_display (" 301.0 KB" , ByteSize :: kb (301 ));
38- assert_display (" 419.0 MB" , ByteSize :: mb (419 ));
39- assert_display (" 518.0 GB" , ByteSize :: gb (518 ));
40- assert_display (" 815.0 TB" , ByteSize :: tb (815 ));
41- assert_display (" 609.0 PB" , ByteSize :: pb (609 ));
42- }
43-
44- fn assert_to_string (expected : & str , b : ByteSize , si : bool ) {
45- assert_eq! (expected . to_string (), b . to_string_as (si ));
46- }
47-
48- #[test]
49- fn test_to_string () {
50- assert_to_string (" 215 B" , ByteSize (215 ), true );
51- assert_to_string (" 215 B" , ByteSize (215 ), false );
52-
53- assert_to_string (" 215 B" , ByteSize :: b (215 ), true );
54- assert_to_string (" 215 B" , ByteSize :: b (215 ), false );
55-
56- assert_to_string (" 1.0 kiB" , ByteSize :: kib (1 ), true );
57- assert_to_string (" 1.0 KB" , ByteSize :: kib (1 ), false );
58-
59- assert_to_string (" 293.9 kiB" , ByteSize :: kb (301 ), true );
60- assert_to_string (" 301.0 KB" , ByteSize :: kb (301 ), false );
61-
62- assert_to_string (" 1.0 MiB" , ByteSize :: mib (1 ), true );
63- assert_to_string (" 1048.6 KB" , ByteSize :: mib (1 ), false );
64-
65- assert_to_string (" 399.6 MiB" , ByteSize :: mb (419 ), true );
66- assert_to_string (" 419.0 MB" , ByteSize :: mb (419 ), false );
67-
68- assert_to_string (" 482.4 GiB" , ByteSize :: gb (518 ), true );
69- assert_to_string (" 518.0 GB" , ByteSize :: gb (518 ), false );
70-
71- assert_to_string (" 741.2 TiB" , ByteSize :: tb (815 ), true );
72- assert_to_string (" 815.0 TB" , ByteSize :: tb (815 ), false );
73-
74- assert_to_string (" 540.9 PiB" , ByteSize :: pb (609 ), true );
75- assert_to_string (" 609.0 PB" , ByteSize :: pb (609 ), false );
76- }
24+ extern crate bytesize;
25+
26+ use bytesize :: {ByteSize , IEC SI };
27+
28+ assert_eq! (" 482.4 GiB" . to_string (), ByteSize :: gb (518 ). humanize (IEC ));
29+ assert_eq! (" 518.0 GB" . to_string (), ByteSize :: gb (518 ). humanize (SI ));
7730```
7831
7932### Arithmetic operations
33+
8034``` rust
8135extern crate bytesize;
8236
8337use bytesize :: ByteSize ;
8438
85- fn byte_arithmetic_operator () {
86- let x = ByteSize :: mb (1 );
87- let y = ByteSize :: kb (100 );
39+ let x = ByteSize :: mb (1 );
40+ let y = ByteSize :: kb (100 );
8841
89- let plus = x + y ;
90- print! ( " {} " , plus );
42+ let sum = x + y ;
43+ assert_eq! ( sum , ByteSize :: kb ( 1100 ) );
9144
92- let minus = ByteSize :: tb (100 ) + ByteSize :: gb (4 );
93- print! (" {}" , minus );
94- }
45+ let product = 10u32 * x ;
46+ assert_eq! (product , ByteSize :: mb (10 ));
9547```
0 commit comments