|
1 | 1 | ## ByteSize
|
2 | 2 |
|
3 |
| -[](https://github.com/hyunsik/bytesize/actions/workflows/ci.yml) |
| 3 | +<!-- prettier-ignore-start --> |
| 4 | + |
| 5 | +[](https://github.com/bytesize-rs/bytesize/actions/workflows/ci.yml) |
4 | 6 | [](https://crates.io/crates/bytesize)
|
5 | 7 |
|
6 |
| -`ByteSize` is a utility for human-readable byte count representations. |
| 8 | +<!-- prettier-ignore-end --> |
| 9 | + |
| 10 | +<!-- cargo-rdme start --> |
| 11 | + |
| 12 | +`ByteSize` is a semantic wrapper for byte count representations. |
7 | 13 |
|
8 | 14 | Features:
|
9 | 15 |
|
10 | 16 | - Pre-defined constants for various size units (e.g., B, Kb, Kib, Mb, Mib, Gb, Gib, ... PB).
|
11 | 17 | - `ByteSize` type which presents size units convertible to different size units.
|
12 | 18 | - Arithmetic operations for `ByteSize`.
|
13 |
| -- FromStr impl for `ByteSize`, allowing to parse from string size representations like 1.5KiB and 521TiB. |
| 19 | +- `FromStr` impl for `ByteSize`, allowing for parsing string size representations like "1.5KiB" and "521TiB". |
14 | 20 | - Serde support for binary and human-readable deserializers like JSON.
|
15 | 21 |
|
16 |
| -[API Documentation](https://docs.rs/bytesize) |
| 22 | +### Examples |
17 | 23 |
|
18 |
| -## Examples |
19 |
| - |
20 |
| -### Human readable representations (SI unit and Binary unit) |
| 24 | +Construction using SI or IEC helpers. |
21 | 25 |
|
22 | 26 | ```rust
|
23 |
| -fn assert_display(expected: &str, b: ByteSize) { |
24 |
| - assert_eq!(expected, format!("{}", b)); |
25 |
| -} |
26 |
| - |
27 |
| -#[test] |
28 |
| -fn test_display() { |
29 |
| - assert_display("215 B", ByteSize::b(215)); |
30 |
| - assert_display("1.0 KiB", ByteSize::kib(1)); |
31 |
| - assert_display("301.0 KiB", ByteSize::kib(301)); |
32 |
| - assert_display("419.0 MiB", ByteSize::mib(419)); |
33 |
| - assert_display("518.0 GiB", ByteSize::gib(518)); |
34 |
| - assert_display("815.0 TiB", ByteSize::tib(815)); |
35 |
| - assert_display("609.0 PiB", ByteSize::pib(609)); |
36 |
| -} |
37 |
| - |
38 |
| -#[test] |
39 |
| -fn test_display_alignment() { |
40 |
| - assert_eq!("|357 B |", format!("|{:10}|", ByteSize(357))); |
41 |
| - assert_eq!("| 357 B|", format!("|{:>10}|", ByteSize(357))); |
42 |
| - assert_eq!("|357 B |", format!("|{:<10}|", ByteSize(357))); |
43 |
| - assert_eq!("| 357 B |", format!("|{:^10}|", ByteSize(357))); |
44 |
| - |
45 |
| - assert_eq!("|-----357 B|", format!("|{:->10}|", ByteSize(357))); |
46 |
| - assert_eq!("|357 B-----|", format!("|{:-<10}|", ByteSize(357))); |
47 |
| - assert_eq!("|--357 B---|", format!("|{:-^10}|", ByteSize(357))); |
48 |
| -} |
49 |
| - |
50 |
| -fn assert_to_string(expected: &str, b: ByteSize, si: bool) { |
51 |
| - assert_eq!(expected.to_string(), b.to_string_as(si)); |
52 |
| -} |
53 |
| - |
54 |
| -#[test] |
55 |
| -fn test_to_string_as() { |
56 |
| - assert_to_string("215 B", ByteSize::b(215), true); |
57 |
| - assert_to_string("215 B", ByteSize::b(215), false); |
58 |
| - |
59 |
| - assert_to_string("1.0 KiB", ByteSize::kib(1), true); |
60 |
| - assert_to_string("1.0 KB", ByteSize::kib(1), false); |
61 |
| - |
62 |
| - assert_to_string("293.9 KiB", ByteSize::kb(301), true); |
63 |
| - assert_to_string("301.0 KB", ByteSize::kb(301), false); |
64 |
| - |
65 |
| - assert_to_string("1.0 MiB", ByteSize::mib(1), true); |
66 |
| - assert_to_string("1048.6 KB", ByteSize::mib(1), false); |
67 |
| - |
68 |
| - // a bug case: https://github.com/flang-project/bytesize/issues/8 |
69 |
| - assert_to_string("1.9 GiB", ByteSize::mib(1907), true); |
70 |
| - assert_to_string("2.0 GB", ByteSize::mib(1908), false); |
71 |
| - |
72 |
| - assert_to_string("399.6 MiB", ByteSize::mb(419), true); |
73 |
| - assert_to_string("419.0 MB", ByteSize::mb(419), false); |
74 |
| - |
75 |
| - assert_to_string("482.4 GiB", ByteSize::gb(518), true); |
76 |
| - assert_to_string("518.0 GB", ByteSize::gb(518), false); |
77 |
| - |
78 |
| - assert_to_string("741.2 TiB", ByteSize::tb(815), true); |
79 |
| - assert_to_string("815.0 TB", ByteSize::tb(815), false); |
80 |
| - |
81 |
| - assert_to_string("540.9 PiB", ByteSize::pb(609), true); |
82 |
| - assert_to_string("609.0 PB", ByteSize::pb(609), false); |
83 |
| -} |
| 27 | +use bytesize::ByteSize; |
| 28 | + |
| 29 | +assert!(ByteSize::kib(4) > ByteSize::kb(4)); |
84 | 30 | ```
|
85 | 31 |
|
86 |
| -### Arithmetic Operations |
| 32 | +Display as human-readable string. |
87 | 33 |
|
88 | 34 | ```rust
|
89 | 35 | use bytesize::ByteSize;
|
90 | 36 |
|
91 |
| -fn byte_arithmetic_operator() { |
92 |
| - let x = ByteSize::mb(1); |
93 |
| - let y = ByteSize::kb(100); |
| 37 | +assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(true)); |
| 38 | +assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(false)); |
| 39 | +``` |
| 40 | + |
| 41 | +Arithmetic operations are supported. |
| 42 | + |
| 43 | +```rust |
| 44 | +use bytesize::ByteSize; |
94 | 45 |
|
95 |
| - let plus = x + y; |
96 |
| - print!("{}", plus); |
| 46 | +let plus = ByteSize::mb(1) + ByteSize::kb(100); |
| 47 | +println!("{plus}"); |
97 | 48 |
|
98 |
| - let minus = ByteSize::tb(100) + ByteSize::gb(4); |
99 |
| - print!("{}", minus); |
100 |
| -} |
| 49 | +let minus = ByteSize::tb(1) - ByteSize::gb(4); |
| 50 | +assert_eq!(ByteSize::gb(996), minus); |
101 | 51 | ```
|
| 52 | + |
| 53 | +<!-- cargo-rdme end --> |
0 commit comments