You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,11 +21,11 @@ This crate provides multiple semantic wrappers and utilities for byte size repre
21
21
## Features
22
22
23
23
*`#![no_std]`-capable, no heap allocation, and no runtime dependencies by default.
24
-
*`BSize`defaults to`usize`, with `BSize8`, `BSize16`, `BSize32`, and `BSize64` aliases for representing byte sizes with different underlying types.
25
-
*`FromStr` impl for `BSize`, allowing for parsing string size representations like "1.5 KiB" and "521 TB".
26
-
*`Display` impl for `BSize`, allowing for formatting byte sizes as human-readable strings in both binary (e.g., "1.5 MiB") and decimal (e.g., "1.5 MB") styles.
24
+
*`ByteSize<T>` wrappers over supported unsigned integer base types, with `BSize`as the`usize` alias and `BSize8`, `BSize16`, `BSize32`, and `BSize64` aliases for fixed-width base types.
25
+
*`FromStr` impl for `ByteSize`, allowing for parsing string size representations like "1.5 KiB" and "521 TB".
26
+
*`Display` impl for `ByteSize`, allowing for formatting byte sizes as human-readable strings in both binary (e.g., "1.5 MiB") and decimal (e.g., "1.5 MB") styles.
27
27
* Optional `serde` support for binary and human-readable format.
28
-
* Optional `nightly` support for generic const unit constructors, allowing calls like `BSize::kib(16_u64)`.
28
+
* Optional `nightly` support for generic const unit constructors, allowing calls like `ByteSize::kib(16_u64)`.
So you don't have to multiply the numbers by hand and rely on comments to indicate the units. This also makes it easier to change the units later if needed.
@@ -85,11 +85,11 @@ The [`bytesize`](https://crates.io/crates/bytesize) crate provides a `ByteSize`
85
85
86
86
I was more than happy to try `bytesize` at first. However, I found that it does not provide a way to specify the underlying integer type for the byte size. It uses `u64` internally, while most of the constants shown above are of type `usize`. This means that I have to convert between `u64` and `usize` frequently, which is not ideal. See [this issue](https://github.com/bytesize-rs/bytesize/issues/135) for more details.
87
87
88
-
What's more, to support calculations between `BSize`and numeric types, this crate implements `BSize::map` for producing a new `BSize`, and exposes the `.0` field for arbitrary calculations from the underlying byte count. This avoids implementing arithmetic traits for calculations between `BSize`and numeric types. The latter would cause confusions like what result type should be used for `ByteSize + u64`. However, `BSize` implements arithmetic traits for calculations between `BSize` and `BSize`, which is more intuitive and less error-prone.
88
+
What's more, to support calculations between byte size wrappers and numeric types, this crate implements `ByteSize::map` for producing a new wrapper, and exposes the `.0` field for arbitrary calculations from the underlying byte count. This avoids implementing arithmetic traits for calculations between byte size wrappers and numeric types. The latter would cause confusions like what result type should be used for `bytesize::ByteSize + u64`. However, `ByteSize` implements arithmetic traits for calculations between wrappers with the same base type, which is more intuitive and less error-prone.
89
89
90
90
```rust
91
-
letresult=ByteSize::kib(4) +64; // Is the result type ByteSize or u64? Why?
92
-
letresult=BSize64::kib(4).map(|b|b+64); // Clearly the result type is BSize.
91
+
letresult=bytesize::ByteSize::kib(4) +64; // Is the result type bytesize::ByteSize or u64? Why?
92
+
letresult=BSize64::kib(4).map(|b|b+64); // Clearly the result type is BSize64.
93
93
letresult=BSize64::kib(4).0+64; // Clearly the result type is u64.
0 commit comments