From 4cc313177aa7a76326b92db46b47e0ee928ba850 Mon Sep 17 00:00:00 2001 From: Sheape Date: Sun, 31 Aug 2025 19:14:19 +0800 Subject: [PATCH 1/2] feat: added exabyte and exbibyte --- src/lib.rs | 30 +++++++++++++++++++++++++++++- src/parse.rs | 6 ++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 8e3709c..0b7e56b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ //! //! Features: //! -//! - Pre-defined constants for various size units (e.g., B, Kb, Kib, Mb, Mib, Gb, Gib, ... PB). +//! - Pre-defined constants for various size units (e.g., B, Kb, Kib, Mb, Mib, Gb, Gib, ... EB). //! - `ByteSize` type which presents size units convertible to different size units. //! - Arithmetic operations for `ByteSize`. //! - `FromStr` impl for `ByteSize`, allowing for parsing string size representations like "1.5KiB" @@ -68,6 +68,8 @@ pub const GB: u64 = 1_000_000_000; pub const TB: u64 = 1_000_000_000_000; /// Number of bytes in 1 petabyte. pub const PB: u64 = 1_000_000_000_000_000; +/// Number of bytes in 1 exabyte. +pub const EB: u64 = 1_000_000_000_000_000_000; /// Number of bytes in 1 kibibyte. pub const KIB: u64 = 1_024; @@ -79,6 +81,8 @@ pub const GIB: u64 = 1_073_741_824; pub const TIB: u64 = 1_099_511_627_776; /// Number of bytes in 1 pebibyte. pub const PIB: u64 = 1_125_899_906_842_624; +/// Number of bytes in 1 exbibyte. +pub const EIB: u64 = 1_152_921_504_606_846_976; /// IEC (binary) units. /// @@ -146,6 +150,16 @@ pub fn pib>(size: V) -> u64 { size.into() * PIB } +/// Converts a quantity of exabytes to bytes. +pub fn eb>(size: V) -> u64 { + size.into() * EB +} + +/// Converts a quantity of exbibytes to bytes. +pub fn eib>(size: V) -> u64 { + size.into() * EIB +} + /// Byte size representation. #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Default)] pub struct ByteSize(pub u64); @@ -217,6 +231,18 @@ impl ByteSize { ByteSize(size * PIB) } + /// Constructs a byte size wrapper from a quantity of exabytes. + #[inline(always)] + pub const fn eb(size: u64) -> ByteSize { + ByteSize(size * EB) + } + + /// Constructs a byte size wrapper from a quantity of exbibytes. + #[inline(always)] + pub const fn eib(size: u64) -> ByteSize { + ByteSize(size * EIB) + } + /// Returns byte count. #[inline(always)] pub const fn as_u64(&self) -> u64 { @@ -459,6 +485,7 @@ mod tests { assert!(ByteSize::mb(1) != ByteSize::kib(1024)); assert!(ByteSize::mb(1) < ByteSize::kib(1024)); assert!(ByteSize::b(0) < ByteSize::tib(1)); + assert!(ByteSize::pib(1) < ByteSize::eb(1)); } #[track_caller] @@ -475,6 +502,7 @@ mod tests { assert_display("518.0 GiB", ByteSize::gib(518)); assert_display("815.0 TiB", ByteSize::tib(815)); assert_display("609.0 PiB", ByteSize::pib(609)); + assert_display("15.0 EiB", ByteSize::eib(15)); } #[test] diff --git a/src/parse.rs b/src/parse.rs index 012aa28..8805099 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -62,12 +62,14 @@ enum Unit { GigaByte, TeraByte, PetaByte, + ExaByte, // power of twos KibiByte, MebiByte, GibiByte, TebiByte, PebiByte, + ExbiByte, } impl Unit { @@ -80,12 +82,14 @@ impl Unit { Self::GigaByte => crate::GB, Self::TeraByte => crate::TB, Self::PetaByte => crate::PB, + Self::ExaByte => crate::EB, // binary units Self::KibiByte => crate::KIB, Self::MebiByte => crate::MIB, Self::GibiByte => crate::GIB, Self::TebiByte => crate::TIB, Self::PebiByte => crate::PIB, + Self::ExbiByte => crate::EIB, } } } @@ -171,12 +175,14 @@ impl str::FromStr for Unit { "g" | "gb" => Ok(Self::GigaByte), "t" | "tb" => Ok(Self::TeraByte), "p" | "pb" => Ok(Self::PetaByte), + "e" | "eb" => Ok(Self::ExaByte), // power of twos "ki" | "kib" => Ok(Self::KibiByte), "mi" | "mib" => Ok(Self::MebiByte), "gi" | "gib" => Ok(Self::GibiByte), "ti" | "tib" => Ok(Self::TebiByte), "pi" | "pib" => Ok(Self::PebiByte), + "ei" | "eib" => Ok(Self::ExbiByte), _ => Err(format!("couldn't parse unit of {:?}", unit)), } } From b21c196b59867efc880b213a5e26f7aaa7e114e6 Mon Sep 17 00:00:00 2001 From: Sheape Date: Mon, 1 Sep 2025 04:45:42 +0800 Subject: [PATCH 2/2] style: fix formatting in `Unit` --- src/parse.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parse.rs b/src/parse.rs index 3734954..a33710e 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -78,7 +78,7 @@ impl Unit { Self::GigaByte => crate::GB, Self::TeraByte => crate::TB, Self::PetaByte => crate::PB, - Self::ExaByte => crate::EB, + Self::ExaByte => crate::EB, // binary units Self::KibiByte => crate::KIB, Self::MebiByte => crate::MIB,