From 16cb5526cf6fb89bf9dcf29a576d051f60eed163 Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 28 Jun 2026 11:50:41 +0800 Subject: [PATCH 1/2] refactor!: rework public APIs Signed-off-by: tison --- CHANGELOG.md | 4 +++- README.md | 19 ++++++++-------- bsize/src/lib.rs | 17 ++++++++++----- bsize/src/types/mod.rs | 44 ++++++++++++++++++++++++++++---------- bsize/src/types/nightly.rs | 3 +-- bsize/src/types/stable.rs | 4 ++-- 6 files changed, 60 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 025988f..cfb518b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,12 +6,14 @@ All significant changes to this software be documented in this file. ### Breaking changes -* Renamed `BSize::with` to `BSize::map`. A new `BSize::with` method has been added that accepts a closure returning arbitrary type, allowing for mapping a `BSize` to any other type. This follows `LocalKey::with`'s naming conventions. +* Renamed the old `BSize::with` mapping API to `BSize::map`. * Removed the `Displayable` trait. `ByteSize` now has a `to_f64` method that is the same as the `Displayable::canonicalize` method. ### New features * Added `nightly` feature for using `BSize` with nightly-only features like `const_ops` and `const_trait_impl`. +* Added a default `usize` underlying type for `BSize`, so `BSize` is equivalent to `BSize` in type positions. +* Added `BSize8`, `BSize16`, `BSize32`, and `BSize64` aliases. * Added `ByteSize::to_f64` for converting supported byte size underlying types to approximate `f64` values. * Added `BSize::as_b` for returning the byte count as an approximate `f64`. diff --git a/README.md b/README.md index 20c6615..7e4b28a 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ This crate provides multiple semantic wrappers and utilities for byte size repre ## Features * `#![no_std]`-capable, no heap allocation, and no runtime dependencies by default. -* `BSize` wrappers over `u8`, `u16`, `u32`, `u64`, and `usize` for representing byte sizes with different underlying types. +* `BSize` defaults to `usize`, with `BSize8`, `BSize16`, `BSize32`, and `BSize64` aliases for representing byte sizes with different underlying types. * `FromStr` impl for `BSize`, allowing for parsing string size representations like "1.5 KiB" and "521 TB". * `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. * Optional `serde` support for binary and human-readable format. @@ -59,10 +59,10 @@ const RESULT_SIZE_LIMIT: usize = 8 * 1024 * 1024 * 1024; // 8 GiB I want them to be: ```rust -const BASE_BLOB_INDEX_SIZE: BSize = BSize::kib(4); -const BASE_BLOCK_SIZE: BSize = BSize::mib(16); -const RESERVED_MEMORY: BSize = BSize::mib(256); -const RESULT_SIZE_LIMIT: BSize = BSize::gib(8); +const BASE_BLOB_INDEX_SIZE: BSize = BSize::::kib(4); +const BASE_BLOCK_SIZE: BSize = BSize::::mib(16); +const RESERVED_MEMORY: BSize = BSize::::mib(256); +const RESULT_SIZE_LIMIT: BSize = BSize::::gib(8); ``` 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,16 +85,15 @@ The [`bytesize`](https://crates.io/crates/bytesize) crate provides a `ByteSize` 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. -What's more, to support calculations between `BSize` and numeric types, this crate implements `BSize::map` for producing a new `BSize`, and `BSize::with` for producing an arbitrary result 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. +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. ```rust let result = ByteSize::kib(4) + 64; // Is the result type ByteSize or u64? Why? -let result = BSize::::kib(4).map(|b| b + 64); // Clearly the result type is BSize. -let result = BSize::::kib(4).0 + 64; // Clearly the result type is u64. -let result = BSize::::kib(4).with(|b| b + 64); // when .0 is cumbersome sometimes, this is more convenient. +let result = BSize64::kib(4).map(|b| b + 64); // Clearly the result type is BSize. +let result = BSize64::kib(4).0 + 64; // Clearly the result type is u64. ``` -There is no `Unit` as well. To obtain a constant for a specific unit, you can use `BSize::::kib(1).0` and this can be resolved at compile time. +There is no `Unit` as well. To obtain a constant for a specific unit, you can use `BSize64::kib(1).0` and this can be resolved at compile time. Finally, the following issues in `bytesize` have been resolved in this crate: diff --git a/bsize/src/lib.rs b/bsize/src/lib.rs index b964584..3dc7ada 100644 --- a/bsize/src/lib.rs +++ b/bsize/src/lib.rs @@ -21,8 +21,8 @@ //! # Features //! //! * `#![no_std]`-capable, no heap allocation, and no runtime dependencies by default. -//! * [`BSize`] wrappers over `u8`, `u16`, `u32`, `u64`, and `usize` for representing byte sizes -//! with different underlying types. +//! * [`BSize`] defaults to `usize`, with [`BSize8`], [`BSize16`], [`BSize32`], and [`BSize64`] +//! aliases for representing byte sizes with different underlying types. //! * `FromStr` impl for `BSize`, allowing for parsing string size representations like "1.5 KiB" //! and "521 TB". //! * [`Display`] impl for `BSize`, allowing for formatting byte sizes as human-readable strings in @@ -39,16 +39,19 @@ //! use bsize::BSize; //! //! assert!(BSize::::kib(4) > BSize::::kb(4)); +//! +//! let size: BSize = BSize::b(4_096); +//! assert_eq!(size.0, 4_096); //! ``` //! //! Parse byte sizes from strings. //! //! ``` -//! use bsize::BSize; +//! use bsize::BSize64; //! -//! let size: BSize = "1.5 MiB".parse().unwrap(); +//! let size: BSize64 = "1.5 MiB".parse().unwrap(); //! -//! assert_eq!(BSize::::mib(1).map(|bytes| bytes + 512 * 1024), size); +//! assert_eq!(BSize64::mib(1).map(|bytes| bytes + 512 * 1024), size); //! ``` //! //! Display as human-readable string. @@ -126,6 +129,10 @@ pub use self::traits::MegaByteSize; pub use self::traits::PetaByteSize; pub use self::traits::TeraByteSize; pub use self::types::BSize; +pub use self::types::BSize8; +pub use self::types::BSize16; +pub use self::types::BSize32; +pub use self::types::BSize64; #[cfg(test)] fn assert_close(actual: f64, expected: f64) { diff --git a/bsize/src/types/mod.rs b/bsize/src/types/mod.rs index 736401b..727c0e9 100644 --- a/bsize/src/types/mod.rs +++ b/bsize/src/types/mod.rs @@ -19,7 +19,19 @@ use crate::traits::ByteSize; /// Byte size representation. #[derive(Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct BSize(pub T); +pub struct BSize(pub T); + +/// Byte size representation backed by `u8`. +pub type BSize8 = BSize; + +/// Byte size representation backed by `u16`. +pub type BSize16 = BSize; + +/// Byte size representation backed by `u32`. +pub type BSize32 = BSize; + +/// Byte size representation backed by `u64`. +pub type BSize64 = BSize; impl fmt::Debug for BSize { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -44,11 +56,6 @@ impl BSize { BSize(f(self.0)) } - /// Calculate a new value with the provided function. - pub fn with(self, f: impl FnOnce(T) -> R) -> R { - f(self.0) - } - /// Constructs a byte size wrapper from a quantity of bytes. #[inline(always)] pub const fn b(size: T) -> Self { @@ -64,6 +71,10 @@ mod stable; #[cfg(test)] mod tests { use super::BSize; + use super::BSize8; + use super::BSize16; + use super::BSize32; + use super::BSize64; use crate::assert_close; #[test] @@ -75,6 +86,22 @@ mod tests { assert_eq!(BSize::::default(), BSize::b(0)); } + #[test] + fn default_type_is_usize() { + let default: BSize = BSize::b(2); + let explicit: BSize = BSize::b(2); + + assert_eq!(default, explicit); + } + + #[test] + fn aliases_use_expected_underlying_types() { + assert_eq!(BSize8::b(2), BSize::::b(2)); + assert_eq!(BSize16::kib(2), BSize::::kib(2)); + assert_eq!(BSize32::gib(2), BSize::::gib(2)); + assert_eq!(BSize64::eib(2), BSize::::eib(2)); + } + #[test] fn constructs_u8_units() { assert_eq!(BSize::::b(2).0, 2); @@ -97,11 +124,6 @@ mod tests { ); } - #[test] - fn with_returns_closure_result() { - assert!(BSize::::kib(4).with(|bytes| bytes == 4_096)); - } - #[test] fn constructs_u16_units() { assert_eq!(BSize::::kb(2).0, 2_000); diff --git a/bsize/src/types/nightly.rs b/bsize/src/types/nightly.rs index 3cf6602..acc3efb 100644 --- a/bsize/src/types/nightly.rs +++ b/bsize/src/types/nightly.rs @@ -25,8 +25,7 @@ impl BSize { /// Returns byte count as bytes. /// /// The result is approximate when the byte count cannot be represented - /// exactly as `f64`. Use `.0` or [`BSize::with`] for the exact underlying - /// integer value. + /// exactly as `f64`. Use `.0` for the exact underlying integer value. #[inline(always)] pub const fn as_b(&self) -> f64 where diff --git a/bsize/src/types/stable.rs b/bsize/src/types/stable.rs index 668cfe4..eee8abc 100644 --- a/bsize/src/types/stable.rs +++ b/bsize/src/types/stable.rs @@ -90,8 +90,8 @@ macroweave::repeat!(Ty in [u8, u16, u32, u64, usize] { /// Returns byte count as bytes. /// /// The result is approximate when the byte count cannot be - /// represented exactly as `f64`. Use `.0` or [`BSize::with`] - /// for the exact underlying integer value. + /// represented exactly as `f64`. Use `.0` for the exact underlying + /// integer value. #[inline(always)] pub const fn as_b(&self) -> f64 { self.0 as f64 From 3dc070dc7ef4cb8f522344212b0db941c06af006 Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 28 Jun 2026 12:08:56 +0800 Subject: [PATCH 2/2] renames Signed-off-by: tison --- CHANGELOG.md | 9 +- README.md | 22 ++--- bsize/src/display.rs | 31 +++---- bsize/src/lib.rs | 46 +++++------ bsize/src/ops/mod.rs | 32 ++++---- bsize/src/ops/nightly.rs | 52 ++++++------ bsize/src/ops/stable.rs | 22 ++--- bsize/src/parse.rs | 24 +++--- bsize/src/serde.rs | 28 +++---- bsize/src/traits/nightly.rs | 24 +++--- bsize/src/traits/stable.rs | 24 +++--- bsize/src/types/mod.rs | 160 +++++++++++++++++------------------- bsize/src/types/nightly.rs | 58 ++++++------- bsize/src/types/stable.rs | 16 ++-- 14 files changed, 268 insertions(+), 280 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfb518b..d0bff8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,14 +7,15 @@ All significant changes to this software be documented in this file. ### Breaking changes * Renamed the old `BSize::with` mapping API to `BSize::map`. -* Removed the `Displayable` trait. `ByteSize` now has a `to_f64` method that is the same as the `Displayable::canonicalize` method. +* Renamed the generic `BSize` wrapper to `ByteSize`. `BSize` is now an alias for `ByteSize`. +* Renamed the `ByteSize` trait to `BaseByteSize`. +* Removed the `Displayable` trait. `BaseByteSize` now has a `to_f64` method that is the same as the `Displayable::canonicalize` method. ### New features -* Added `nightly` feature for using `BSize` with nightly-only features like `const_ops` and `const_trait_impl`. -* Added a default `usize` underlying type for `BSize`, so `BSize` is equivalent to `BSize` in type positions. +* Added `nightly` feature for using `ByteSize` with nightly-only features like `const_ops` and `const_trait_impl`. * Added `BSize8`, `BSize16`, `BSize32`, and `BSize64` aliases. -* Added `ByteSize::to_f64` for converting supported byte size underlying types to approximate `f64` values. +* Added `BaseByteSize::to_f64` for converting supported byte size base types to approximate `f64` values. * Added `BSize::as_b` for returning the byte count as an approximate `f64`. ## v0.2.1 (2026-06-27) diff --git a/README.md b/README.md index 7e4b28a..47baae3 100644 --- a/README.md +++ b/README.md @@ -21,11 +21,11 @@ This crate provides multiple semantic wrappers and utilities for byte size repre ## Features * `#![no_std]`-capable, no heap allocation, and no runtime dependencies by default. -* `BSize` defaults to `usize`, with `BSize8`, `BSize16`, `BSize32`, and `BSize64` aliases for representing byte sizes with different underlying types. -* `FromStr` impl for `BSize`, allowing for parsing string size representations like "1.5 KiB" and "521 TB". -* `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. +* `ByteSize` 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. +* `FromStr` impl for `ByteSize`, allowing for parsing string size representations like "1.5 KiB" and "521 TB". +* `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. * Optional `serde` support for binary and human-readable format. -* Optional `nightly` support for generic const unit constructors, allowing calls like `BSize::kib(16_u64)`. +* Optional `nightly` support for generic const unit constructors, allowing calls like `ByteSize::kib(16_u64)`. ## Documentation @@ -59,10 +59,10 @@ const RESULT_SIZE_LIMIT: usize = 8 * 1024 * 1024 * 1024; // 8 GiB I want them to be: ```rust -const BASE_BLOB_INDEX_SIZE: BSize = BSize::::kib(4); -const BASE_BLOCK_SIZE: BSize = BSize::::mib(16); -const RESERVED_MEMORY: BSize = BSize::::mib(256); -const RESULT_SIZE_LIMIT: BSize = BSize::::gib(8); +const BASE_BLOB_INDEX_SIZE: BSize = BSize::kib(4); +const BASE_BLOCK_SIZE: BSize = BSize::mib(16); +const RESERVED_MEMORY: BSize = BSize::mib(256); +const RESULT_SIZE_LIMIT: BSize = BSize::gib(8); ``` 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` 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. -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. +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. ```rust -let result = ByteSize::kib(4) + 64; // Is the result type ByteSize or u64? Why? -let result = BSize64::kib(4).map(|b| b + 64); // Clearly the result type is BSize. +let result = bytesize::ByteSize::kib(4) + 64; // Is the result type bytesize::ByteSize or u64? Why? +let result = BSize64::kib(4).map(|b| b + 64); // Clearly the result type is BSize64. let result = BSize64::kib(4).0 + 64; // Clearly the result type is u64. ``` diff --git a/bsize/src/display.rs b/bsize/src/display.rs index 51bb3d6..c5e3285 100644 --- a/bsize/src/display.rs +++ b/bsize/src/display.rs @@ -15,18 +15,18 @@ use core::fmt; use core::fmt::Write as _; -use crate::BSize; +use crate::BaseByteSize; use crate::ByteSize; /// Create a [`Display`] instance for displaying a byte size. /// /// See [`Display`] for examples. Use [`Display::new`] when the byte count is already represented /// as an `f64`. -pub fn display(size: impl ByteSize) -> Display { +pub fn display(size: impl BaseByteSize) -> Display { Display::new(size.to_f64()) } -impl BSize { +impl ByteSize { /// Returns a [`Display`] wrapper. /// /// See [`Display`] for examples. @@ -37,7 +37,7 @@ impl BSize { /// Display wrapper for formatting byte sizes as human-readable strings. /// -/// You may create this wrapper with [`Display::new`], [`display`], or [`BSize::display`], then +/// You may create this wrapper with [`Display::new`], [`display`], or [`ByteSize::display`], then /// pass custom [`DisplayOptions`] with [`Display::options`]. /// /// # Examples @@ -45,22 +45,16 @@ impl BSize { /// Display with the [`DisplayOptions::BINARY`] and [`DisplayOptions::DECIMAL`] presets. /// /// ``` -/// use bsize::BSize; +/// use bsize::BSize64; /// /// assert_eq!( /// "41.0 KiB", -/// BSize::::kb(42).display().to_string(), // default to binary +/// BSize64::kb(42).display().to_string(), // default to binary /// ); /// -/// assert_eq!( -/// "1.0 MiB", -/// BSize::::mib(1).display().binary().to_string(), -/// ); +/// assert_eq!("1.0 MiB", BSize64::mib(1).display().binary().to_string(),); /// -/// assert_eq!( -/// "42.0 kB", -/// BSize::::kb(42).display().decimal().to_string(), -/// ); +/// assert_eq!("42.0 kB", BSize64::kb(42).display().decimal().to_string(),); /// ``` /// /// The free [`display`] function accepts any supported integer byte size. @@ -81,12 +75,9 @@ impl BSize { /// Use standard formatter precision to control the number of fractional digits. /// /// ``` -/// use bsize::BSize; +/// use bsize::BSize64; /// -/// assert_eq!( -/// "1.54 KiB", -/// format!("{:.2}", BSize::::b(1575).display()) -/// ); +/// assert_eq!("1.54 KiB", format!("{:.2}", BSize64::b(1575).display())); /// assert_eq!("1.575 KiB", format!("{:.3}", bsize::display(1613u64))); /// ``` /// @@ -324,7 +315,7 @@ impl Display { /// Create a [`Display`] instance from a byte count. /// /// This constructor is useful when the byte count is already represented as an `f64`. For - /// supported integer byte counts, use [`display`] or [`BSize::display`]. + /// supported integer byte counts, use [`display`] or [`ByteSize::display`]. /// /// # Examples /// diff --git a/bsize/src/lib.rs b/bsize/src/lib.rs index 3dc7ada..f8e1e2a 100644 --- a/bsize/src/lib.rs +++ b/bsize/src/lib.rs @@ -21,15 +21,16 @@ //! # Features //! //! * `#![no_std]`-capable, no heap allocation, and no runtime dependencies by default. -//! * [`BSize`] defaults to `usize`, with [`BSize8`], [`BSize16`], [`BSize32`], and [`BSize64`] -//! aliases for representing byte sizes with different underlying types. -//! * `FromStr` impl for `BSize`, allowing for parsing string size representations like "1.5 KiB" +//! * Generic [`ByteSize`] wrappers over supported unsigned integer base types, with [`BSize`] as +//! the `usize` alias and [`BSize8`], [`BSize16`], [`BSize32`], and [`BSize64`] as shorter aliases +//! for fixed-width base types. +//! * `FromStr` impl for `ByteSize`, allowing for parsing string size representations like "1.5 KiB" //! and "521 TB". -//! * [`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. +//! * [`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. //! * Optional `serde` support for binary and human-readable format. //! * Optional `nightly` support for generic const unit constructors, allowing calls like -//! `BSize::kib(16_u64)`. +//! `ByteSize::kib(16_u64)`. //! //! # Examples //! @@ -38,7 +39,7 @@ //! ``` //! use bsize::BSize; //! -//! assert!(BSize::::kib(4) > BSize::::kb(4)); +//! assert!(BSize::kib(4) > BSize::kb(4)); //! //! let size: BSize = BSize::b(4_096); //! assert_eq!(size.0, 4_096); @@ -62,15 +63,9 @@ //! use bsize::DisplayOptions; //! use bsize::DisplayScale; //! -//! assert_eq!( -//! "518.0 GiB", -//! BSize::::gib(518).display().binary().to_string() -//! ); +//! assert_eq!("518.0 GiB", BSize::gib(518).display().binary().to_string()); //! -//! assert_eq!( -//! "556.2 GB", -//! BSize::::gib(518).display().decimal().to_string() -//! ); +//! assert_eq!("556.2 GB", BSize::gib(518).display().decimal().to_string()); //! //! let network_units = DisplayOptions::DECIMAL //! .base_unit(DisplayBaseUnit::Bit) @@ -84,11 +79,11 @@ //! ``` //! use bsize::BSize; //! -//! let plus = BSize::::mb(1) + BSize::::kb(100); +//! let plus = BSize::mb(1) + BSize::kb(100); //! println!("{plus}"); //! -//! let minus = BSize::::tb(1) - BSize::::gb(4); -//! assert_eq!(BSize::::gb(996), minus); +//! let minus = BSize::tb(1) - BSize::gb(4); +//! assert_eq!(BSize::gb(996), minus); //! ``` //! //! Arithmetic operations over the underlying types are supported. @@ -96,7 +91,7 @@ //!``` //! use bsize::BSize; //! -//! let size = BSize::::mb(1); +//! let size = BSize::mb(1); //! let size = size.map(|b| b * 4); // 4x scale //! println!("{size}"); //! ``` @@ -121,7 +116,7 @@ pub use self::display::DisplayScale; pub use self::display::DisplayUnitSystem; pub use self::display::display; pub use self::parse::ParseError; -pub use self::traits::ByteSize; +pub use self::traits::BaseByteSize; pub use self::traits::ExaByteSize; pub use self::traits::GigaByteSize; pub use self::traits::KiloByteSize; @@ -133,6 +128,7 @@ pub use self::types::BSize8; pub use self::types::BSize16; pub use self::types::BSize32; pub use self::types::BSize64; +pub use self::types::ByteSize; #[cfg(test)] fn assert_close(actual: f64, expected: f64) { @@ -152,7 +148,7 @@ mod property_tests { use super::*; - impl quickcheck::Arbitrary for BSize { + impl quickcheck::Arbitrary for ByteSize { fn arbitrary(g: &mut quickcheck::Gen) -> Self { Self(u64::arbitrary(g)) } @@ -160,16 +156,16 @@ mod property_tests { quickcheck::quickcheck! { fn parsing_never_panics(size: String) -> bool { - let _ = size.parse::>(); + let _ = size.parse::>(); true } - fn to_string_never_blank(size: BSize) -> bool { + fn to_string_never_blank(size: ByteSize) -> bool { !size.to_string().is_empty() } - fn string_round_trip(size: BSize) -> bool { - size.to_string().parse::>().unwrap() == size + fn string_round_trip(size: ByteSize) -> bool { + size.to_string().parse::>().unwrap() == size } } } diff --git a/bsize/src/ops/mod.rs b/bsize/src/ops/mod.rs index 388269c..66bf07d 100644 --- a/bsize/src/ops/mod.rs +++ b/bsize/src/ops/mod.rs @@ -20,36 +20,40 @@ mod stable; #[cfg(test)] mod tests { use crate::BSize; + use crate::BSize8; + use crate::BSize16; + use crate::BSize32; + use crate::BSize64; #[test] fn adds_byte_sizes() { - assert_eq!((BSize::(3) + BSize(5)).0, 8); - assert_eq!((BSize::(3) + BSize(5)).0, 8); - assert_eq!((BSize::(3) + BSize(5)).0, 8); - assert_eq!((BSize::(3) + BSize(5)).0, 8); - assert_eq!((BSize::(3) + BSize(5)).0, 8); + assert_eq!((BSize8::b(3) + BSize8::b(5)).0, 8); + assert_eq!((BSize16::b(3) + BSize16::b(5)).0, 8); + assert_eq!((BSize32::b(3) + BSize32::b(5)).0, 8); + assert_eq!((BSize64::b(3) + BSize64::b(5)).0, 8); + assert_eq!((BSize::b(3) + BSize::b(5)).0, 8); } #[test] fn add_assigns_byte_sizes() { - let mut size = BSize::(3); - size += BSize(5); + let mut size = BSize::b(3); + size += BSize::b(5); assert_eq!(size.0, 8); } #[test] fn subtracts_byte_sizes() { - assert_eq!((BSize::(8) - BSize(5)).0, 3); - assert_eq!((BSize::(8) - BSize(5)).0, 3); - assert_eq!((BSize::(8) - BSize(5)).0, 3); - assert_eq!((BSize::(8) - BSize(5)).0, 3); - assert_eq!((BSize::(8) - BSize(5)).0, 3); + assert_eq!((BSize8::b(8) - BSize8::b(5)).0, 3); + assert_eq!((BSize16::b(8) - BSize16::b(5)).0, 3); + assert_eq!((BSize32::b(8) - BSize32::b(5)).0, 3); + assert_eq!((BSize64::b(8) - BSize64::b(5)).0, 3); + assert_eq!((BSize::b(8) - BSize::b(5)).0, 3); } #[test] fn sub_assigns_byte_sizes() { - let mut size = BSize::(8); - size -= BSize(5); + let mut size = BSize::b(8); + size -= BSize::b(5); assert_eq!(size.0, 3); } } diff --git a/bsize/src/ops/nightly.rs b/bsize/src/ops/nightly.rs index 94cc58e..6a7697d 100644 --- a/bsize/src/ops/nightly.rs +++ b/bsize/src/ops/nightly.rs @@ -14,70 +14,70 @@ use core::ops; -use crate::traits::ByteSize; -use crate::types::BSize; +use crate::traits::BaseByteSize; +use crate::types::ByteSize; -const impl ops::Add> for BSize +const impl ops::Add> for ByteSize where - T: [const] ByteSize + [const] ops::Add, + T: [const] BaseByteSize + [const] ops::Add, { type Output = Self; #[inline(always)] - fn add(self, rhs: BSize) -> Self::Output { - BSize(self.0 + rhs.0) + fn add(self, rhs: ByteSize) -> Self::Output { + ByteSize(self.0 + rhs.0) } } -const impl ops::AddAssign> for BSize +const impl ops::AddAssign> for ByteSize where - T: [const] ByteSize + [const] ops::AddAssign, + T: [const] BaseByteSize + [const] ops::AddAssign, { #[inline(always)] - fn add_assign(&mut self, rhs: BSize) { + fn add_assign(&mut self, rhs: ByteSize) { self.0 += rhs.0; } } -const impl ops::Sub> for BSize +const impl ops::Sub> for ByteSize where - T: [const] ByteSize + [const] ops::Sub, + T: [const] BaseByteSize + [const] ops::Sub, { type Output = Self; #[inline(always)] - fn sub(self, rhs: BSize) -> Self::Output { - BSize(self.0 - rhs.0) + fn sub(self, rhs: ByteSize) -> Self::Output { + ByteSize(self.0 - rhs.0) } } -const impl ops::SubAssign> for BSize +const impl ops::SubAssign> for ByteSize where - T: [const] ByteSize + [const] ops::SubAssign, + T: [const] BaseByteSize + [const] ops::SubAssign, { #[inline(always)] - fn sub_assign(&mut self, rhs: BSize) { + fn sub_assign(&mut self, rhs: ByteSize) { self.0 -= rhs.0; } } #[cfg(test)] mod tests { - use crate::BSize; + use crate::BSize64; #[test] fn arithmetic_is_const() { - const SUM: BSize = BSize::b(3_u64) + BSize::b(5_u64); - const DIFFERENCE: BSize = BSize::b(8_u64) - BSize::b(5_u64); - const ASSIGNED: BSize = { - let mut size = BSize::b(3_u64); - size += BSize::b(5_u64); - size -= BSize::b(2_u64); + const SUM: BSize64 = BSize64::b(3) + BSize64::b(5); + const DIFFERENCE: BSize64 = BSize64::b(8) - BSize64::b(5); + const ASSIGNED: BSize64 = { + let mut size = BSize64::b(3); + size += BSize64::b(5); + size -= BSize64::b(2); size }; - assert_eq!(SUM, BSize::b(8)); - assert_eq!(DIFFERENCE, BSize::b(3)); - assert_eq!(ASSIGNED, BSize::b(6)); + assert_eq!(SUM, BSize64::b(8)); + assert_eq!(DIFFERENCE, BSize64::b(3)); + assert_eq!(ASSIGNED, BSize64::b(6)); } } diff --git a/bsize/src/ops/stable.rs b/bsize/src/ops/stable.rs index 49dcecb..aa1a6c4 100644 --- a/bsize/src/ops/stable.rs +++ b/bsize/src/ops/stable.rs @@ -14,37 +14,37 @@ use core::ops; -use crate::types::BSize; +use crate::types::ByteSize; macroweave::repeat!(Ty in [u8, u16, u32, u64, usize] { - impl ops::Add> for BSize { + impl ops::Add> for ByteSize { type Output = Self; #[inline(always)] - fn add(self, rhs: BSize) -> Self::Output { - BSize(self.0 + rhs.0) + fn add(self, rhs: ByteSize) -> Self::Output { + ByteSize(self.0 + rhs.0) } } - impl ops::AddAssign> for BSize { + impl ops::AddAssign> for ByteSize { #[inline(always)] - fn add_assign(&mut self, rhs: BSize) { + fn add_assign(&mut self, rhs: ByteSize) { self.0 += rhs.0; } } - impl ops::Sub> for BSize { + impl ops::Sub> for ByteSize { type Output = Self; #[inline(always)] - fn sub(self, rhs: BSize) -> Self::Output { - BSize(self.0 - rhs.0) + fn sub(self, rhs: ByteSize) -> Self::Output { + ByteSize(self.0 - rhs.0) } } - impl ops::SubAssign> for BSize { + impl ops::SubAssign> for ByteSize { #[inline(always)] - fn sub_assign(&mut self, rhs: BSize) { + fn sub_assign(&mut self, rhs: ByteSize) { self.0 -= rhs.0; } } diff --git a/bsize/src/parse.rs b/bsize/src/parse.rs index 8b181e1..8795309 100644 --- a/bsize/src/parse.rs +++ b/bsize/src/parse.rs @@ -16,7 +16,7 @@ use core::convert::TryFrom; use core::fmt; use core::str::FromStr; -use crate::BSize; +use crate::BaseByteSize; use crate::ByteSize; /// The error returned when parsing a byte size fails. @@ -44,7 +44,7 @@ impl fmt::Display for ParseError { impl core::error::Error for ParseError {} macroweave::repeat!(Ty in [u8, u16, u32, u64, usize] { - impl FromStr for BSize { + impl FromStr for ByteSize { type Err = ParseError; fn from_str(s: &str) -> Result { @@ -53,12 +53,12 @@ macroweave::repeat!(Ty in [u8, u16, u32, u64, usize] { } }); -fn bsize_from_u64(size: u64) -> Result, ParseError> +fn bsize_from_u64(size: u64) -> Result, ParseError> where - T: ByteSize + TryFrom, + T: BaseByteSize + TryFrom, { T::try_from(size) - .map(BSize) + .map(ByteSize) .map_err(|_| ParseError::Overflow) } @@ -204,17 +204,17 @@ mod tests { use super::*; fn assert_parse_ok(input: &str, expected: u64) { - let actual = BSize::::from_str(input).unwrap(); - let expected = BSize::(expected); + let actual = ByteSize::::from_str(input).unwrap(); + let expected = ByteSize::(expected); assert_eq!(actual, expected, "input: {input:?}"); - let round_trip = actual.to_string().parse::>().unwrap(); + let round_trip = actual.to_string().parse::>().unwrap(); assert_eq!(round_trip, expected, "input: {input:?}"); } fn assert_parse_err(input: &str, expected: ParseError) { assert_eq!( - input.parse::>(), + input.parse::>(), Err(expected), "input: {input:?}", ); @@ -315,8 +315,8 @@ mod tests { assert_parse_err(input, ParseError::Overflow); } - assert_eq!("256".parse::>(), Err(ParseError::Overflow)); - assert_eq!("64 KiB".parse::>(), Err(ParseError::Overflow)); - assert_eq!("4GiB".parse::>(), Err(ParseError::Overflow)); + assert_eq!("256".parse::>(), Err(ParseError::Overflow)); + assert_eq!("64 KiB".parse::>(), Err(ParseError::Overflow)); + assert_eq!("4GiB".parse::>(), Err(ParseError::Overflow)); } } diff --git a/bsize/src/serde.rs b/bsize/src/serde.rs index 6d7a151..ba53d67 100644 --- a/bsize/src/serde.rs +++ b/bsize/src/serde.rs @@ -16,12 +16,12 @@ use core::fmt; use serde_core::de; -use crate::BSize; +use crate::ByteSize; macro_rules! impl_serialize { ($($ty:ty),* $(,)?) => { $( - impl serde_core::Serialize for BSize<$ty> { + impl serde_core::Serialize for ByteSize<$ty> { fn serialize(&self, ser: S) -> Result where S: serde_core::Serializer, @@ -42,7 +42,7 @@ impl_serialize!(u8, u16, u32, u64, usize); macro_rules! impl_deserialize { ($($ty:ty => $deserialize:ident),* $(,)?) => { $( - impl<'de> serde_core::Deserialize<'de> for BSize<$ty> { + impl<'de> serde_core::Deserialize<'de> for ByteSize<$ty> { fn deserialize(de: D) -> Result where D: serde_core::Deserializer<'de>, @@ -50,7 +50,7 @@ macro_rules! impl_deserialize { struct Visitor; impl de::Visitor<'_> for Visitor { - type Value = BSize<$ty>; + type Value = ByteSize<$ty>; fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_str("an integer or string") @@ -59,7 +59,7 @@ macro_rules! impl_deserialize { fn visit_i64(self, value: i64) -> Result { if let Ok(val) = u64::try_from(value) { if val <= <$ty>::MAX as u64 { - Ok(BSize(val as $ty)) + Ok(ByteSize(val as $ty)) } else { Err(E::invalid_value( de::Unexpected::Signed(value), @@ -76,7 +76,7 @@ macro_rules! impl_deserialize { fn visit_u64(self, value: u64) -> Result { if value <= <$ty>::MAX as u64 { - Ok(BSize(value as $ty)) + Ok(ByteSize(value as $ty)) } else { Err(E::invalid_value( de::Unexpected::Unsigned(value), @@ -129,35 +129,35 @@ mod tests { use serde::Deserialize; use serde::Serialize; - use super::*; + use crate::BSize; #[test] fn test_serde() { #[derive(Serialize, Deserialize)] struct S { - x: BSize, + x: BSize, } let s = serde_json::from_str::(r#"{ "x": "5 B" }"#).unwrap(); - assert_eq!(s.x, BSize::(5)); + assert_eq!(s.x, BSize::b(5)); let s = serde_json::from_str::(r#"{ "x": 1048576 }"#).unwrap(); - assert_eq!(s.x, "1 MiB".parse::>().unwrap()); + assert_eq!(s.x, "1 MiB".parse::().unwrap()); let s = toml::from_str::(r#"x = "2.5 MiB""#).unwrap(); - assert_eq!(s.x, "2.5 MiB".parse::>().unwrap()); + assert_eq!(s.x, "2.5 MiB".parse::().unwrap()); // i64 MAX let s = toml::from_str::(r#"x = "9223372036854775807""#).unwrap(); - assert_eq!(s.x, "9223372036854775807".parse::>().unwrap()); + assert_eq!(s.x, "9223372036854775807".parse::().unwrap()); } #[test] fn test_serde_json() { - let json = serde_json::to_string(&BSize::::mib(1)).unwrap(); + let json = serde_json::to_string(&BSize::mib(1)).unwrap(); assert_eq!(json, "\"1048576 B\""); - let deserialized = serde_json::from_str::>(&json).unwrap(); + let deserialized = serde_json::from_str::(&json).unwrap(); assert_eq!(deserialized.0, 1048576); } } diff --git a/bsize/src/traits/nightly.rs b/bsize/src/traits/nightly.rs index 1ad77d1..2c6a65a 100644 --- a/bsize/src/traits/nightly.rs +++ b/bsize/src/traits/nightly.rs @@ -16,14 +16,16 @@ use core::ops::Mul; use super::private; -/// A marker trait for all supported byte size underlying types. -pub const trait ByteSize: private::Sealed + Clone + Copy + Sized { - /// Returns the byte size as an approximate `f64`. +/// A sealed trait for integer types that can back [`ByteSize`](crate::ByteSize). +/// +/// This trait is implemented only for the unsigned integer types supported by this crate. +pub const trait BaseByteSize: private::Sealed + Clone + Copy + Sized { + /// Returns this byte count as an approximate `f64`. fn to_f64(&self) -> f64; } -/// A trait for byte size payload types that can represent kilobyte-scale units. -pub const trait KiloByteSize: [const] ByteSize + [const] Mul { +/// Provides kilobyte-scale unit constants for supported backing integer types. +pub const trait KiloByteSize: [const] BaseByteSize + [const] Mul { /// Number of bytes in one kilobyte. const KB: Self; @@ -31,7 +33,7 @@ pub const trait KiloByteSize: [const] ByteSize + [const] Mul { const KIB: Self; } -/// A trait for byte size payload types that can represent megabyte-scale units. +/// Provides megabyte-scale unit constants for supported backing integer types. pub const trait MegaByteSize: [const] KiloByteSize { /// Number of bytes in one megabyte. const MB: Self; @@ -40,7 +42,7 @@ pub const trait MegaByteSize: [const] KiloByteSize { const MIB: Self; } -/// A trait for byte size payload types that can represent gigabyte-scale units. +/// Provides gigabyte-scale unit constants for supported backing integer types. pub const trait GigaByteSize: [const] MegaByteSize { /// Number of bytes in one gigabyte. const GB: Self; @@ -49,7 +51,7 @@ pub const trait GigaByteSize: [const] MegaByteSize { const GIB: Self; } -/// A trait for byte size payload types that can represent terabyte-scale units. +/// Provides terabyte-scale unit constants for supported backing integer types. pub const trait TeraByteSize: [const] GigaByteSize { /// Number of bytes in one terabyte. const TB: Self; @@ -58,7 +60,7 @@ pub const trait TeraByteSize: [const] GigaByteSize { const TIB: Self; } -/// A trait for byte size payload types that can represent petabyte-scale units. +/// Provides petabyte-scale unit constants for supported backing integer types. pub const trait PetaByteSize: [const] TeraByteSize { /// Number of bytes in one petabyte. const PB: Self; @@ -67,7 +69,7 @@ pub const trait PetaByteSize: [const] TeraByteSize { const PIB: Self; } -/// A trait for byte size payload types that can represent exabyte-scale units. +/// Provides exabyte-scale unit constants for supported backing integer types. pub const trait ExaByteSize: [const] PetaByteSize { /// Number of bytes in one exabyte. const EB: Self; @@ -77,7 +79,7 @@ pub const trait ExaByteSize: [const] PetaByteSize { } macroweave::repeat!(Ty in [u8, u16, u32, u64, usize] { - const impl ByteSize for Ty { + const impl BaseByteSize for Ty { fn to_f64(&self) -> f64 { *self as f64 } diff --git a/bsize/src/traits/stable.rs b/bsize/src/traits/stable.rs index 982f0d0..6ab2cca 100644 --- a/bsize/src/traits/stable.rs +++ b/bsize/src/traits/stable.rs @@ -16,14 +16,16 @@ use core::ops::Mul; use super::private; -/// A marker trait for all supported byte size underlying types. -pub trait ByteSize: private::Sealed + Clone + Copy + Sized { - /// Returns the byte size as an approximate `f64`. +/// A sealed trait for integer types that can back [`ByteSize`](crate::ByteSize). +/// +/// This trait is implemented only for the unsigned integer types supported by this crate. +pub trait BaseByteSize: private::Sealed + Clone + Copy + Sized { + /// Returns this byte count as an approximate `f64`. fn to_f64(&self) -> f64; } -/// A trait for byte size payload types that can represent kilobyte-scale units. -pub trait KiloByteSize: ByteSize + Mul { +/// Provides kilobyte-scale unit constants for supported backing integer types. +pub trait KiloByteSize: BaseByteSize + Mul { /// Number of bytes in one kilobyte. const KB: Self; @@ -31,7 +33,7 @@ pub trait KiloByteSize: ByteSize + Mul { const KIB: Self; } -/// A trait for byte size payload types that can represent megabyte-scale units. +/// Provides megabyte-scale unit constants for supported backing integer types. pub trait MegaByteSize: KiloByteSize { /// Number of bytes in one megabyte. const MB: Self; @@ -40,7 +42,7 @@ pub trait MegaByteSize: KiloByteSize { const MIB: Self; } -/// A trait for byte size payload types that can represent gigabyte-scale units. +/// Provides gigabyte-scale unit constants for supported backing integer types. pub trait GigaByteSize: MegaByteSize { /// Number of bytes in one gigabyte. const GB: Self; @@ -49,7 +51,7 @@ pub trait GigaByteSize: MegaByteSize { const GIB: Self; } -/// A trait for byte size payload types that can represent terabyte-scale units. +/// Provides terabyte-scale unit constants for supported backing integer types. pub trait TeraByteSize: GigaByteSize { /// Number of bytes in one terabyte. const TB: Self; @@ -58,7 +60,7 @@ pub trait TeraByteSize: GigaByteSize { const TIB: Self; } -/// A trait for byte size payload types that can represent petabyte-scale units. +/// Provides petabyte-scale unit constants for supported backing integer types. pub trait PetaByteSize: TeraByteSize { /// Number of bytes in one petabyte. const PB: Self; @@ -67,7 +69,7 @@ pub trait PetaByteSize: TeraByteSize { const PIB: Self; } -/// A trait for byte size payload types that can represent exabyte-scale units. +/// Provides exabyte-scale unit constants for supported backing integer types. pub trait ExaByteSize: PetaByteSize { /// Number of bytes in one exabyte. const EB: Self; @@ -77,7 +79,7 @@ pub trait ExaByteSize: PetaByteSize { } macroweave::repeat!(Ty in [u8, u16, u32, u64, usize] { - impl ByteSize for Ty { + impl BaseByteSize for Ty { fn to_f64(&self) -> f64 { *self as f64 } diff --git a/bsize/src/types/mod.rs b/bsize/src/types/mod.rs index 727c0e9..a24bd85 100644 --- a/bsize/src/types/mod.rs +++ b/bsize/src/types/mod.rs @@ -15,51 +15,54 @@ use core::any::type_name; use core::fmt; -use crate::traits::ByteSize; +use crate::traits::BaseByteSize; /// Byte size representation. #[derive(Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct BSize(pub T); +pub struct ByteSize(pub T); + +/// Byte size representation backed by `usize`. +pub type BSize = ByteSize; /// Byte size representation backed by `u8`. -pub type BSize8 = BSize; +pub type BSize8 = ByteSize; /// Byte size representation backed by `u16`. -pub type BSize16 = BSize; +pub type BSize16 = ByteSize; /// Byte size representation backed by `u32`. -pub type BSize32 = BSize; +pub type BSize32 = ByteSize; /// Byte size representation backed by `u64`. -pub type BSize64 = BSize; +pub type BSize64 = ByteSize; -impl fmt::Debug for BSize { +impl fmt::Debug for ByteSize { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "BSize<{}>({:?})", type_name::(), self.0) + write!(f, "ByteSize<{}>({:?})", type_name::(), self.0) } } -impl fmt::Display for BSize { +impl fmt::Display for ByteSize { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Stick to a base scale, so that users would not be surprised by: // - // println!("{}", BSize::::kb(42)) + // println!("{}", BSize::kb(42)) // // returns "41.0 KiB" rather than "42.0 KB". write!(f, "{} B", self.0) } } -impl BSize { +impl ByteSize { /// Calculate a new byte size with the provided function, returning a new struct. pub fn map(self, f: impl FnOnce(T) -> T) -> Self { - BSize(f(self.0)) + ByteSize(f(self.0)) } /// Constructs a byte size wrapper from a quantity of bytes. #[inline(always)] pub const fn b(size: T) -> Self { - BSize(size) + ByteSize(size) } } @@ -75,65 +78,63 @@ mod tests { use super::BSize16; use super::BSize32; use super::BSize64; + use super::ByteSize; use crate::assert_close; #[test] fn defaults() { - assert_eq!(BSize::::default(), BSize::b(0)); - assert_eq!(BSize::::default(), BSize::b(0)); - assert_eq!(BSize::::default(), BSize::b(0)); - assert_eq!(BSize::::default(), BSize::b(0)); - assert_eq!(BSize::::default(), BSize::b(0)); + assert_eq!(BSize8::default(), BSize8::b(0)); + assert_eq!(BSize16::default(), BSize16::b(0)); + assert_eq!(BSize32::default(), BSize32::b(0)); + assert_eq!(BSize64::default(), BSize64::b(0)); + assert_eq!(BSize::default(), BSize::b(0)); } #[test] - fn default_type_is_usize() { + fn bsize_alias_is_usize() { let default: BSize = BSize::b(2); - let explicit: BSize = BSize::b(2); + let explicit: ByteSize = ByteSize::b(2); assert_eq!(default, explicit); } #[test] fn aliases_use_expected_underlying_types() { - assert_eq!(BSize8::b(2), BSize::::b(2)); - assert_eq!(BSize16::kib(2), BSize::::kib(2)); - assert_eq!(BSize32::gib(2), BSize::::gib(2)); - assert_eq!(BSize64::eib(2), BSize::::eib(2)); + assert_eq!(BSize8::b(2), ByteSize::::b(2)); + assert_eq!(BSize16::kib(2), ByteSize::::kib(2)); + assert_eq!(BSize32::gib(2), ByteSize::::gib(2)); + assert_eq!(BSize64::eib(2), ByteSize::::eib(2)); } #[test] fn constructs_u8_units() { - assert_eq!(BSize::::b(2).0, 2); + assert_eq!(BSize8::b(2).0, 2); } #[test] fn returns_byte_units() { - assert_close(BSize::::b(2).as_b(), 2.0); - assert_close(BSize::::b(2).as_b(), 2.0); - assert_close(BSize::::b(2).as_b(), 2.0); - assert_close(BSize::::b(2).as_b(), 2.0); - assert_close(BSize::::b(2).as_b(), 2.0); + assert_close(BSize8::b(2).as_b(), 2.0); + assert_close(BSize16::b(2).as_b(), 2.0); + assert_close(BSize32::b(2).as_b(), 2.0); + assert_close(BSize64::b(2).as_b(), 2.0); + assert_close(BSize::b(2).as_b(), 2.0); } #[test] fn maps_underlying_byte_count() { - assert_eq!( - BSize::::kib(4).map(|bytes| bytes + 64), - BSize::b(4_160), - ); + assert_eq!(BSize64::kib(4).map(|bytes| bytes + 64), BSize64::b(4_160),); } #[test] fn constructs_u16_units() { - assert_eq!(BSize::::kb(2).0, 2_000); - assert_eq!(BSize::::kib(2).0, 2_048); + assert_eq!(BSize16::kb(2).0, 2_000); + assert_eq!(BSize16::kib(2).0, 2_048); } #[test] fn returns_u16_units() { - assert_close(BSize::::kb(2).as_kb(), 2.0); - assert_close(BSize::::kib(2).as_kib(), 2.0); + assert_close(BSize16::kb(2).as_kb(), 2.0); + assert_close(BSize16::kib(2).as_kib(), 2.0); } #[test] @@ -142,23 +143,20 @@ mod tests { let kb = 1_000u16; let kib = 1_024u16; - assert_close(BSize::::b(bytes).as_kb(), (bytes as f64) / (kb as f64)); - assert_close( - BSize::::b(bytes).as_kib(), - (bytes as f64) / (kib as f64), - ); + assert_close(BSize16::b(bytes).as_kb(), (bytes as f64) / (kb as f64)); + assert_close(BSize16::b(bytes).as_kib(), (bytes as f64) / (kib as f64)); } #[test] fn constructs_u32_units() { - assert_eq!(BSize::::gb(2).0, 2_000_000_000); - assert_eq!(BSize::::gib(2).0, 2_147_483_648); + assert_eq!(BSize32::gb(2).0, 2_000_000_000); + assert_eq!(BSize32::gib(2).0, 2_147_483_648); } #[test] fn returns_u32_units() { - assert_close(BSize::::gb(2).as_gb(), 2.0); - assert_close(BSize::::gib(2).as_gib(), 2.0); + assert_close(BSize32::gb(2).as_gb(), 2.0); + assert_close(BSize32::gib(2).as_gib(), 2.0); } #[test] @@ -167,61 +165,58 @@ mod tests { let gb = 1_000_000_000u32; let gib = 1_073_741_824u32; - assert_close(BSize::::b(bytes).as_gb(), (bytes as f64) / (gb as f64)); - assert_close( - BSize::::b(bytes).as_gib(), - (bytes as f64) / (gib as f64), - ); + assert_close(BSize32::b(bytes).as_gb(), (bytes as f64) / (gb as f64)); + assert_close(BSize32::b(bytes).as_gib(), (bytes as f64) / (gib as f64)); } #[test] fn constructs_u64_units() { - assert_eq!(BSize::::eb(2).0, 2_000_000_000_000_000_000); - assert_eq!(BSize::::eib(2).0, 2_305_843_009_213_693_952); + assert_eq!(BSize64::eb(2).0, 2_000_000_000_000_000_000); + assert_eq!(BSize64::eib(2).0, 2_305_843_009_213_693_952); } #[test] fn returns_u64_units() { - assert_close(BSize::::eib(2).as_eib(), 2.0); + assert_close(BSize64::eib(2).as_eib(), 2.0); } #[cfg(target_pointer_width = "16")] #[test] fn returns_usize_units() { - assert_eq!(BSize::::kb(2).0, 2_000); - assert_eq!(BSize::::kib(2).0, 2_048); - assert_close(BSize::::kb(2).as_kb(), 2.0); - assert_close(BSize::::kib(2).as_kib(), 2.0); + assert_eq!(BSize::kb(2).0, 2_000); + assert_eq!(BSize::kib(2).0, 2_048); + assert_close(BSize::kb(2).as_kb(), 2.0); + assert_close(BSize::kib(2).as_kib(), 2.0); } #[cfg(target_pointer_width = "32")] #[test] fn returns_usize_units() { - assert_eq!(BSize::::kb(2).0, 2_000); - assert_eq!(BSize::::kib(2).0, 2_048); - assert_close(BSize::::kb(2).as_kb(), 2.0); - assert_close(BSize::::kib(2).as_kib(), 2.0); - assert_eq!(BSize::::gb(2).0, 2_000_000_000); - assert_eq!(BSize::::gib(2).0, 2_147_483_648); - assert_close(BSize::::gb(2).as_gb(), 2.0); - assert_close(BSize::::gib(2).as_gib(), 2.0); + assert_eq!(BSize::kb(2).0, 2_000); + assert_eq!(BSize::kib(2).0, 2_048); + assert_close(BSize::kb(2).as_kb(), 2.0); + assert_close(BSize::kib(2).as_kib(), 2.0); + assert_eq!(BSize::gb(2).0, 2_000_000_000); + assert_eq!(BSize::gib(2).0, 2_147_483_648); + assert_close(BSize::gb(2).as_gb(), 2.0); + assert_close(BSize::gib(2).as_gib(), 2.0); } #[cfg(target_pointer_width = "64")] #[test] fn returns_usize_units() { - assert_eq!(BSize::::kb(2).0, 2_000); - assert_eq!(BSize::::kib(2).0, 2_048); - assert_close(BSize::::kb(2).as_kb(), 2.0); - assert_close(BSize::::kib(2).as_kib(), 2.0); - assert_eq!(BSize::::gb(2).0, 2_000_000_000); - assert_eq!(BSize::::gib(2).0, 2_147_483_648); - assert_close(BSize::::gb(2).as_gb(), 2.0); - assert_close(BSize::::gib(2).as_gib(), 2.0); - assert_eq!(BSize::::eb(2).0, 2_000_000_000_000_000_000); - assert_eq!(BSize::::eib(2).0, 2_305_843_009_213_693_952); - assert_close(BSize::::eb(2).as_eb(), 2.0); - assert_close(BSize::::eib(2).as_eib(), 2.0); + assert_eq!(BSize::kb(2).0, 2_000); + assert_eq!(BSize::kib(2).0, 2_048); + assert_close(BSize::kb(2).as_kb(), 2.0); + assert_close(BSize::kib(2).as_kib(), 2.0); + assert_eq!(BSize::gb(2).0, 2_000_000_000); + assert_eq!(BSize::gib(2).0, 2_147_483_648); + assert_close(BSize::gb(2).as_gb(), 2.0); + assert_close(BSize::gib(2).as_gib(), 2.0); + assert_eq!(BSize::eb(2).0, 2_000_000_000_000_000_000); + assert_eq!(BSize::eib(2).0, 2_305_843_009_213_693_952); + assert_close(BSize::eb(2).as_eb(), 2.0); + assert_close(BSize::eib(2).as_eib(), 2.0); } #[test] @@ -230,10 +225,7 @@ mod tests { let eb = 1_000_000_000_000_000_000u64; let eib = 1_152_921_504_606_846_976u64; - assert_close(BSize::::b(bytes).as_eb(), (bytes as f64) / (eb as f64)); - assert_close( - BSize::::b(bytes).as_eib(), - (bytes as f64) / (eib as f64), - ); + assert_close(BSize64::b(bytes).as_eb(), (bytes as f64) / (eb as f64)); + assert_close(BSize64::b(bytes).as_eib(), (bytes as f64) / (eib as f64)); } } diff --git a/bsize/src/types/nightly.rs b/bsize/src/types/nightly.rs index acc3efb..ed85b93 100644 --- a/bsize/src/types/nightly.rs +++ b/bsize/src/types/nightly.rs @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use super::BSize; -use crate::traits::ByteSize; +use super::ByteSize; +use crate::traits::BaseByteSize; use crate::traits::ExaByteSize; use crate::traits::GigaByteSize; use crate::traits::KiloByteSize; @@ -21,7 +21,7 @@ use crate::traits::MegaByteSize; use crate::traits::PetaByteSize; use crate::traits::TeraByteSize; -impl BSize { +impl ByteSize { /// Returns byte count as bytes. /// /// The result is approximate when the byte count cannot be represented @@ -29,20 +29,20 @@ impl BSize { #[inline(always)] pub const fn as_b(&self) -> f64 where - T: [const] ByteSize, + T: [const] BaseByteSize, { self.0.to_f64() } } -impl BSize { +impl ByteSize { /// Constructs a byte size wrapper from a quantity of `kb` units. #[inline(always)] pub const fn kb(size: T) -> Self where T: [const] KiloByteSize, { - BSize(size * T::KB) + ByteSize(size * T::KB) } /// Constructs a byte size wrapper from a quantity of `kib` units. @@ -51,7 +51,7 @@ impl BSize { where T: [const] KiloByteSize, { - BSize(size * T::KIB) + ByteSize(size * T::KIB) } /// Returns byte count as kilobytes. @@ -79,14 +79,14 @@ impl BSize { } } -impl BSize { +impl ByteSize { /// Constructs a byte size wrapper from a quantity of `mb` units. #[inline(always)] pub const fn mb(size: T) -> Self where T: [const] MegaByteSize, { - BSize(size * T::MB) + ByteSize(size * T::MB) } /// Constructs a byte size wrapper from a quantity of `mib` units. @@ -95,7 +95,7 @@ impl BSize { where T: [const] MegaByteSize, { - BSize(size * T::MIB) + ByteSize(size * T::MIB) } /// Returns byte count as megabytes. @@ -123,14 +123,14 @@ impl BSize { } } -impl BSize { +impl ByteSize { /// Constructs a byte size wrapper from a quantity of `gb` units. #[inline(always)] pub const fn gb(size: T) -> Self where T: [const] GigaByteSize, { - BSize(size * T::GB) + ByteSize(size * T::GB) } /// Constructs a byte size wrapper from a quantity of `gib` units. @@ -139,7 +139,7 @@ impl BSize { where T: [const] GigaByteSize, { - BSize(size * T::GIB) + ByteSize(size * T::GIB) } /// Returns byte count as gigabytes. @@ -167,14 +167,14 @@ impl BSize { } } -impl BSize { +impl ByteSize { /// Constructs a byte size wrapper from a quantity of `tb` units. #[inline(always)] pub const fn tb(size: T) -> Self where T: [const] TeraByteSize, { - BSize(size * T::TB) + ByteSize(size * T::TB) } /// Constructs a byte size wrapper from a quantity of `tib` units. @@ -183,7 +183,7 @@ impl BSize { where T: [const] TeraByteSize, { - BSize(size * T::TIB) + ByteSize(size * T::TIB) } /// Returns byte count as terabytes. @@ -211,14 +211,14 @@ impl BSize { } } -impl BSize { +impl ByteSize { /// Constructs a byte size wrapper from a quantity of `pb` units. #[inline(always)] pub const fn pb(size: T) -> Self where T: [const] PetaByteSize, { - BSize(size * T::PB) + ByteSize(size * T::PB) } /// Constructs a byte size wrapper from a quantity of `pib` units. @@ -227,7 +227,7 @@ impl BSize { where T: [const] PetaByteSize, { - BSize(size * T::PIB) + ByteSize(size * T::PIB) } /// Returns byte count as petabytes. @@ -255,14 +255,14 @@ impl BSize { } } -impl BSize { +impl ByteSize { /// Constructs a byte size wrapper from a quantity of `eb` units. #[inline(always)] pub const fn eb(size: T) -> Self where T: [const] ExaByteSize, { - BSize(size * T::EB) + ByteSize(size * T::EB) } /// Constructs a byte size wrapper from a quantity of `eib` units. @@ -271,7 +271,7 @@ impl BSize { where T: [const] ExaByteSize, { - BSize(size * T::EIB) + ByteSize(size * T::EIB) } /// Returns byte count as exabytes. @@ -301,26 +301,26 @@ impl BSize { #[cfg(test)] mod tests { - use super::BSize; + use super::ByteSize; use crate::assert_close; #[test] fn infers_constructor_type_from_argument() { - assert_eq!(BSize::kib(16_u64), BSize::b(16 * 1_024)); - assert_eq!(BSize::mib(16_u32), BSize::b(16 * 1_048_576)); + assert_eq!(ByteSize::kib(16_u64), ByteSize::b(16 * 1_024)); + assert_eq!(ByteSize::mib(16_u32), ByteSize::b(16 * 1_048_576)); } #[test] fn inferred_constructor_is_const() { - const SIZE: BSize = BSize::kib(16_u64); + const SIZE: ByteSize = ByteSize::kib(16_u64); - assert_eq!(SIZE, BSize::b(16 * 1_024)); + assert_eq!(SIZE, ByteSize::b(16 * 1_024)); } #[test] fn inferred_accessors_are_const() { - const BYTES: f64 = BSize::b(16_u64).as_b(); - const KIB: f64 = BSize::kib(16_u64).as_kib(); + const BYTES: f64 = ByteSize::b(16_u64).as_b(); + const KIB: f64 = ByteSize::kib(16_u64).as_kib(); assert_close(BYTES, 16.0); assert_close(KIB, 16.0); diff --git a/bsize/src/types/stable.rs b/bsize/src/types/stable.rs index eee8abc..9064c63 100644 --- a/bsize/src/types/stable.rs +++ b/bsize/src/types/stable.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use super::BSize; +use super::ByteSize; macroweave::repeat!((Ty, Name, Trait, Size) in [ (u16, kb, KiloByteSize, KB), @@ -36,7 +36,7 @@ macroweave::repeat!((Ty, Name, Trait, Size) in [ (u64, eb, ExaByteSize, EB), (u64, eib, ExaByteSize, EIB), ] { - impl BSize { + impl ByteSize { #[doc = concat!( "Constructs a byte size wrapper from a quantity of `", stringify!(Name), @@ -44,7 +44,7 @@ macroweave::repeat!((Ty, Name, Trait, Size) in [ )] #[inline(always)] pub const fn Name(size: Ty) -> Self { - BSize(size * ::Size) + ByteSize(size * ::Size) } } }); @@ -72,7 +72,7 @@ macroweave::repeat!((PointerWidth, Name, Trait, Size) in [ ("64", eib, ExaByteSize, EIB), ] { #[cfg(target_pointer_width = PointerWidth)] - impl BSize { + impl ByteSize { #[doc = concat!( "Constructs a byte size wrapper from a quantity of `", stringify!(Name), @@ -80,13 +80,13 @@ macroweave::repeat!((PointerWidth, Name, Trait, Size) in [ )] #[inline(always)] pub const fn Name(size: usize) -> Self { - BSize(size * ::Size) + ByteSize(size * ::Size) } } }); macroweave::repeat!(Ty in [u8, u16, u32, u64, usize] { - impl BSize { + impl ByteSize { /// Returns byte count as bytes. /// /// The result is approximate when the byte count cannot be @@ -121,7 +121,7 @@ macroweave::repeat!((Ty, Name, Trait, Size, Unit) in [ (u64, as_eb, ExaByteSize, EB, "exabytes"), (u64, as_eib, ExaByteSize, EIB, "exbibytes"), ] { - impl BSize { + impl ByteSize { #[doc = concat!("Returns byte count as ", Unit, ".")] /// /// The result is approximate when the byte count cannot be @@ -156,7 +156,7 @@ macroweave::repeat!((PointerWidth, Name, Trait, Size, Unit) in [ ("64", as_eib, ExaByteSize, EIB, "exbibytes"), ] { #[cfg(target_pointer_width = PointerWidth)] - impl BSize { + impl ByteSize { #[doc = concat!("Returns byte count as ", Unit, ".")] /// /// The result is approximate when the byte count cannot be