Skip to content

Commit 3dc070d

Browse files
committed
renames
Signed-off-by: tison <wander4096@gmail.com>
1 parent 16cb552 commit 3dc070d

14 files changed

Lines changed: 268 additions & 280 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ All significant changes to this software be documented in this file.
77
### Breaking changes
88

99
* Renamed the old `BSize::with` mapping API to `BSize::map`.
10-
* Removed the `Displayable` trait. `ByteSize` now has a `to_f64` method that is the same as the `Displayable::canonicalize` method.
10+
* Renamed the generic `BSize<T>` wrapper to `ByteSize<T>`. `BSize` is now an alias for `ByteSize<usize>`.
11+
* Renamed the `ByteSize` trait to `BaseByteSize`.
12+
* Removed the `Displayable` trait. `BaseByteSize` now has a `to_f64` method that is the same as the `Displayable::canonicalize` method.
1113

1214
### New features
1315

14-
* Added `nightly` feature for using `BSize` with nightly-only features like `const_ops` and `const_trait_impl`.
15-
* Added a default `usize` underlying type for `BSize`, so `BSize` is equivalent to `BSize<usize>` in type positions.
16+
* Added `nightly` feature for using `ByteSize` with nightly-only features like `const_ops` and `const_trait_impl`.
1617
* Added `BSize8`, `BSize16`, `BSize32`, and `BSize64` aliases.
17-
* Added `ByteSize::to_f64` for converting supported byte size underlying types to approximate `f64` values.
18+
* Added `BaseByteSize::to_f64` for converting supported byte size base types to approximate `f64` values.
1819
* Added `BSize::as_b` for returning the byte count as an approximate `f64`.
1920

2021
## v0.2.1 (2026-06-27)

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ This crate provides multiple semantic wrappers and utilities for byte size repre
2121
## Features
2222

2323
* `#![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.
2727
* 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)`.
2929

3030
## Documentation
3131

@@ -59,10 +59,10 @@ const RESULT_SIZE_LIMIT: usize = 8 * 1024 * 1024 * 1024; // 8 GiB
5959
I want them to be:
6060

6161
```rust
62-
const BASE_BLOB_INDEX_SIZE: BSize = BSize::<usize>::kib(4);
63-
const BASE_BLOCK_SIZE: BSize = BSize::<usize>::mib(16);
64-
const RESERVED_MEMORY: BSize = BSize::<usize>::mib(256);
65-
const RESULT_SIZE_LIMIT: BSize = BSize::<usize>::gib(8);
62+
const BASE_BLOB_INDEX_SIZE: BSize = BSize::kib(4);
63+
const BASE_BLOCK_SIZE: BSize = BSize::mib(16);
64+
const RESERVED_MEMORY: BSize = BSize::mib(256);
65+
const RESULT_SIZE_LIMIT: BSize = BSize::gib(8);
6666
```
6767

6868
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`
8585

8686
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.
8787

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.
8989

9090
```rust
91-
let result = ByteSize::kib(4) + 64; // Is the result type ByteSize or u64? Why?
92-
let result = BSize64::kib(4).map(|b| b + 64); // Clearly the result type is BSize.
91+
let result = bytesize::ByteSize::kib(4) + 64; // Is the result type bytesize::ByteSize or u64? Why?
92+
let result = BSize64::kib(4).map(|b| b + 64); // Clearly the result type is BSize64.
9393
let result = BSize64::kib(4).0 + 64; // Clearly the result type is u64.
9494
```
9595

bsize/src/display.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
use core::fmt;
1616
use core::fmt::Write as _;
1717

18-
use crate::BSize;
18+
use crate::BaseByteSize;
1919
use crate::ByteSize;
2020

2121
/// Create a [`Display`] instance for displaying a byte size.
2222
///
2323
/// See [`Display`] for examples. Use [`Display::new`] when the byte count is already represented
2424
/// as an `f64`.
25-
pub fn display(size: impl ByteSize) -> Display {
25+
pub fn display(size: impl BaseByteSize) -> Display {
2626
Display::new(size.to_f64())
2727
}
2828

29-
impl<T: ByteSize> BSize<T> {
29+
impl<T: BaseByteSize> ByteSize<T> {
3030
/// Returns a [`Display`] wrapper.
3131
///
3232
/// See [`Display`] for examples.
@@ -37,30 +37,24 @@ impl<T: ByteSize> BSize<T> {
3737

3838
/// Display wrapper for formatting byte sizes as human-readable strings.
3939
///
40-
/// You may create this wrapper with [`Display::new`], [`display`], or [`BSize::display`], then
40+
/// You may create this wrapper with [`Display::new`], [`display`], or [`ByteSize::display`], then
4141
/// pass custom [`DisplayOptions`] with [`Display::options`].
4242
///
4343
/// # Examples
4444
///
4545
/// Display with the [`DisplayOptions::BINARY`] and [`DisplayOptions::DECIMAL`] presets.
4646
///
4747
/// ```
48-
/// use bsize::BSize;
48+
/// use bsize::BSize64;
4949
///
5050
/// assert_eq!(
5151
/// "41.0 KiB",
52-
/// BSize::<u64>::kb(42).display().to_string(), // default to binary
52+
/// BSize64::kb(42).display().to_string(), // default to binary
5353
/// );
5454
///
55-
/// assert_eq!(
56-
/// "1.0 MiB",
57-
/// BSize::<u64>::mib(1).display().binary().to_string(),
58-
/// );
55+
/// assert_eq!("1.0 MiB", BSize64::mib(1).display().binary().to_string(),);
5956
///
60-
/// assert_eq!(
61-
/// "42.0 kB",
62-
/// BSize::<u64>::kb(42).display().decimal().to_string(),
63-
/// );
57+
/// assert_eq!("42.0 kB", BSize64::kb(42).display().decimal().to_string(),);
6458
/// ```
6559
///
6660
/// The free [`display`] function accepts any supported integer byte size.
@@ -81,12 +75,9 @@ impl<T: ByteSize> BSize<T> {
8175
/// Use standard formatter precision to control the number of fractional digits.
8276
///
8377
/// ```
84-
/// use bsize::BSize;
78+
/// use bsize::BSize64;
8579
///
86-
/// assert_eq!(
87-
/// "1.54 KiB",
88-
/// format!("{:.2}", BSize::<u64>::b(1575).display())
89-
/// );
80+
/// assert_eq!("1.54 KiB", format!("{:.2}", BSize64::b(1575).display()));
9081
/// assert_eq!("1.575 KiB", format!("{:.3}", bsize::display(1613u64)));
9182
/// ```
9283
///
@@ -324,7 +315,7 @@ impl Display {
324315
/// Create a [`Display`] instance from a byte count.
325316
///
326317
/// This constructor is useful when the byte count is already represented as an `f64`. For
327-
/// supported integer byte counts, use [`display`] or [`BSize::display`].
318+
/// supported integer byte counts, use [`display`] or [`ByteSize::display`].
328319
///
329320
/// # Examples
330321
///

bsize/src/lib.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@
2121
//! # Features
2222
//!
2323
//! * `#![no_std]`-capable, no heap allocation, and no runtime dependencies by default.
24-
//! * [`BSize`] defaults to `usize`, with [`BSize8`], [`BSize16`], [`BSize32`], and [`BSize64`]
25-
//! aliases for representing byte sizes with different underlying types.
26-
//! * `FromStr` impl for `BSize`, allowing for parsing string size representations like "1.5 KiB"
24+
//! * Generic [`ByteSize`] wrappers over supported unsigned integer base types, with [`BSize`] as
25+
//! the `usize` alias and [`BSize8`], [`BSize16`], [`BSize32`], and [`BSize64`] as shorter aliases
26+
//! for fixed-width base types.
27+
//! * `FromStr` impl for `ByteSize`, allowing for parsing string size representations like "1.5 KiB"
2728
//! and "521 TB".
28-
//! * [`Display`] impl for `BSize`, allowing for formatting byte sizes as human-readable strings in
29-
//! both binary (e.g., "1.5 MiB") and decimal (e.g., "1.5 MB") styles.
29+
//! * [`Display`] impl for `ByteSize`, allowing for formatting byte sizes as human-readable strings
30+
//! in both binary (e.g., "1.5 MiB") and decimal (e.g., "1.5 MB") styles.
3031
//! * Optional `serde` support for binary and human-readable format.
3132
//! * Optional `nightly` support for generic const unit constructors, allowing calls like
32-
//! `BSize::kib(16_u64)`.
33+
//! `ByteSize::kib(16_u64)`.
3334
//!
3435
//! # Examples
3536
//!
@@ -38,7 +39,7 @@
3839
//! ```
3940
//! use bsize::BSize;
4041
//!
41-
//! assert!(BSize::<usize>::kib(4) > BSize::<usize>::kb(4));
42+
//! assert!(BSize::kib(4) > BSize::kb(4));
4243
//!
4344
//! let size: BSize = BSize::b(4_096);
4445
//! assert_eq!(size.0, 4_096);
@@ -62,15 +63,9 @@
6263
//! use bsize::DisplayOptions;
6364
//! use bsize::DisplayScale;
6465
//!
65-
//! assert_eq!(
66-
//! "518.0 GiB",
67-
//! BSize::<usize>::gib(518).display().binary().to_string()
68-
//! );
66+
//! assert_eq!("518.0 GiB", BSize::gib(518).display().binary().to_string());
6967
//!
70-
//! assert_eq!(
71-
//! "556.2 GB",
72-
//! BSize::<usize>::gib(518).display().decimal().to_string()
73-
//! );
68+
//! assert_eq!("556.2 GB", BSize::gib(518).display().decimal().to_string());
7469
//!
7570
//! let network_units = DisplayOptions::DECIMAL
7671
//! .base_unit(DisplayBaseUnit::Bit)
@@ -84,19 +79,19 @@
8479
//! ```
8580
//! use bsize::BSize;
8681
//!
87-
//! let plus = BSize::<usize>::mb(1) + BSize::<usize>::kb(100);
82+
//! let plus = BSize::mb(1) + BSize::kb(100);
8883
//! println!("{plus}");
8984
//!
90-
//! let minus = BSize::<usize>::tb(1) - BSize::<usize>::gb(4);
91-
//! assert_eq!(BSize::<usize>::gb(996), minus);
85+
//! let minus = BSize::tb(1) - BSize::gb(4);
86+
//! assert_eq!(BSize::gb(996), minus);
9287
//! ```
9388
//!
9489
//! Arithmetic operations over the underlying types are supported.
9590
//!
9691
//!```
9792
//! use bsize::BSize;
9893
//!
99-
//! let size = BSize::<usize>::mb(1);
94+
//! let size = BSize::mb(1);
10095
//! let size = size.map(|b| b * 4); // 4x scale
10196
//! println!("{size}");
10297
//! ```
@@ -121,7 +116,7 @@ pub use self::display::DisplayScale;
121116
pub use self::display::DisplayUnitSystem;
122117
pub use self::display::display;
123118
pub use self::parse::ParseError;
124-
pub use self::traits::ByteSize;
119+
pub use self::traits::BaseByteSize;
125120
pub use self::traits::ExaByteSize;
126121
pub use self::traits::GigaByteSize;
127122
pub use self::traits::KiloByteSize;
@@ -133,6 +128,7 @@ pub use self::types::BSize8;
133128
pub use self::types::BSize16;
134129
pub use self::types::BSize32;
135130
pub use self::types::BSize64;
131+
pub use self::types::ByteSize;
136132

137133
#[cfg(test)]
138134
fn assert_close(actual: f64, expected: f64) {
@@ -152,24 +148,24 @@ mod property_tests {
152148

153149
use super::*;
154150

155-
impl quickcheck::Arbitrary for BSize<u64> {
151+
impl quickcheck::Arbitrary for ByteSize<u64> {
156152
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
157153
Self(u64::arbitrary(g))
158154
}
159155
}
160156

161157
quickcheck::quickcheck! {
162158
fn parsing_never_panics(size: String) -> bool {
163-
let _ = size.parse::<BSize<u64>>();
159+
let _ = size.parse::<ByteSize<u64>>();
164160
true
165161
}
166162

167-
fn to_string_never_blank(size: BSize<u64>) -> bool {
163+
fn to_string_never_blank(size: ByteSize<u64>) -> bool {
168164
!size.to_string().is_empty()
169165
}
170166

171-
fn string_round_trip(size: BSize<u64>) -> bool {
172-
size.to_string().parse::<BSize<u64>>().unwrap() == size
167+
fn string_round_trip(size: ByteSize<u64>) -> bool {
168+
size.to_string().parse::<ByteSize<u64>>().unwrap() == size
173169
}
174170
}
175171
}

bsize/src/ops/mod.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,40 @@ mod stable;
2020
#[cfg(test)]
2121
mod tests {
2222
use crate::BSize;
23+
use crate::BSize8;
24+
use crate::BSize16;
25+
use crate::BSize32;
26+
use crate::BSize64;
2327

2428
#[test]
2529
fn adds_byte_sizes() {
26-
assert_eq!((BSize::<u8>(3) + BSize(5)).0, 8);
27-
assert_eq!((BSize::<u16>(3) + BSize(5)).0, 8);
28-
assert_eq!((BSize::<u32>(3) + BSize(5)).0, 8);
29-
assert_eq!((BSize::<u64>(3) + BSize(5)).0, 8);
30-
assert_eq!((BSize::<usize>(3) + BSize(5)).0, 8);
30+
assert_eq!((BSize8::b(3) + BSize8::b(5)).0, 8);
31+
assert_eq!((BSize16::b(3) + BSize16::b(5)).0, 8);
32+
assert_eq!((BSize32::b(3) + BSize32::b(5)).0, 8);
33+
assert_eq!((BSize64::b(3) + BSize64::b(5)).0, 8);
34+
assert_eq!((BSize::b(3) + BSize::b(5)).0, 8);
3135
}
3236

3337
#[test]
3438
fn add_assigns_byte_sizes() {
35-
let mut size = BSize::<usize>(3);
36-
size += BSize(5);
39+
let mut size = BSize::b(3);
40+
size += BSize::b(5);
3741
assert_eq!(size.0, 8);
3842
}
3943

4044
#[test]
4145
fn subtracts_byte_sizes() {
42-
assert_eq!((BSize::<u8>(8) - BSize(5)).0, 3);
43-
assert_eq!((BSize::<u16>(8) - BSize(5)).0, 3);
44-
assert_eq!((BSize::<u32>(8) - BSize(5)).0, 3);
45-
assert_eq!((BSize::<u64>(8) - BSize(5)).0, 3);
46-
assert_eq!((BSize::<usize>(8) - BSize(5)).0, 3);
46+
assert_eq!((BSize8::b(8) - BSize8::b(5)).0, 3);
47+
assert_eq!((BSize16::b(8) - BSize16::b(5)).0, 3);
48+
assert_eq!((BSize32::b(8) - BSize32::b(5)).0, 3);
49+
assert_eq!((BSize64::b(8) - BSize64::b(5)).0, 3);
50+
assert_eq!((BSize::b(8) - BSize::b(5)).0, 3);
4751
}
4852

4953
#[test]
5054
fn sub_assigns_byte_sizes() {
51-
let mut size = BSize::<usize>(8);
52-
size -= BSize(5);
55+
let mut size = BSize::b(8);
56+
size -= BSize::b(5);
5357
assert_eq!(size.0, 3);
5458
}
5559
}

0 commit comments

Comments
 (0)