Skip to content

Commit e816797

Browse files
Sheaperobjtede
andauthored
feat: add exabyte and exbibyte support (#101)
Co-authored-by: Rob Ede <[email protected]>
1 parent 3b89d01 commit e816797

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/lib.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! Features:
44
//!
5-
//! - Pre-defined constants for various size units (e.g., B, Kb, Kib, Mb, Mib, Gb, Gib, ... PB).
5+
//! - Pre-defined constants for various size units (e.g., B, Kb, Kib, Mb, Mib, Gb, Gib, ... EB).
66
//! - `ByteSize` type which presents size units convertible to different size units.
77
//! - Arithmetic operations for `ByteSize`.
88
//! - `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;
6868
pub const TB: u64 = 1_000_000_000_000;
6969
/// Number of bytes in 1 petabyte.
7070
pub const PB: u64 = 1_000_000_000_000_000;
71+
/// Number of bytes in 1 exabyte.
72+
pub const EB: u64 = 1_000_000_000_000_000_000;
7173

7274
/// Number of bytes in 1 kibibyte.
7375
pub const KIB: u64 = 1_024;
@@ -79,6 +81,8 @@ pub const GIB: u64 = 1_073_741_824;
7981
pub const TIB: u64 = 1_099_511_627_776;
8082
/// Number of bytes in 1 pebibyte.
8183
pub const PIB: u64 = 1_125_899_906_842_624;
84+
/// Number of bytes in 1 exbibyte.
85+
pub const EIB: u64 = 1_152_921_504_606_846_976;
8286

8387
/// IEC (binary) units.
8488
///
@@ -146,6 +150,16 @@ pub fn pib<V: Into<u64>>(size: V) -> u64 {
146150
size.into() * PIB
147151
}
148152

153+
/// Converts a quantity of exabytes to bytes.
154+
pub fn eb<V: Into<u64>>(size: V) -> u64 {
155+
size.into() * EB
156+
}
157+
158+
/// Converts a quantity of exbibytes to bytes.
159+
pub fn eib<V: Into<u64>>(size: V) -> u64 {
160+
size.into() * EIB
161+
}
162+
149163
/// Byte size representation.
150164
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Default)]
151165
pub struct ByteSize(pub u64);
@@ -217,6 +231,18 @@ impl ByteSize {
217231
ByteSize(size * PIB)
218232
}
219233

234+
/// Constructs a byte size wrapper from a quantity of exabytes.
235+
#[inline(always)]
236+
pub const fn eb(size: u64) -> ByteSize {
237+
ByteSize(size * EB)
238+
}
239+
240+
/// Constructs a byte size wrapper from a quantity of exbibytes.
241+
#[inline(always)]
242+
pub const fn eib(size: u64) -> ByteSize {
243+
ByteSize(size * EIB)
244+
}
245+
220246
/// Returns byte count.
221247
#[inline(always)]
222248
pub const fn as_u64(&self) -> u64 {
@@ -459,6 +485,7 @@ mod tests {
459485
assert!(ByteSize::mb(1) != ByteSize::kib(1024));
460486
assert!(ByteSize::mb(1) < ByteSize::kib(1024));
461487
assert!(ByteSize::b(0) < ByteSize::tib(1));
488+
assert!(ByteSize::pib(1) < ByteSize::eb(1));
462489
}
463490

464491
#[track_caller]
@@ -475,6 +502,7 @@ mod tests {
475502
assert_display("518.0 GiB", ByteSize::gib(518));
476503
assert_display("815.0 TiB", ByteSize::tib(815));
477504
assert_display("609.0 PiB", ByteSize::pib(609));
505+
assert_display("15.0 EiB", ByteSize::eib(15));
478506
}
479507

480508
#[test]

src/parse.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ enum Unit {
5858
GigaByte,
5959
TeraByte,
6060
PetaByte,
61+
ExaByte,
6162
// power of twos
6263
KibiByte,
6364
MebiByte,
6465
GibiByte,
6566
TebiByte,
6667
PebiByte,
68+
ExbiByte,
6769
}
6870

6971
impl Unit {
@@ -76,12 +78,14 @@ impl Unit {
7678
Self::GigaByte => crate::GB,
7779
Self::TeraByte => crate::TB,
7880
Self::PetaByte => crate::PB,
81+
Self::ExaByte => crate::EB,
7982
// binary units
8083
Self::KibiByte => crate::KIB,
8184
Self::MebiByte => crate::MIB,
8285
Self::GibiByte => crate::GIB,
8386
Self::TebiByte => crate::TIB,
8487
Self::PebiByte => crate::PIB,
88+
Self::ExbiByte => crate::EIB,
8589
}
8690
}
8791
}
@@ -167,12 +171,14 @@ impl str::FromStr for Unit {
167171
"g" | "gb" => Ok(Self::GigaByte),
168172
"t" | "tb" => Ok(Self::TeraByte),
169173
"p" | "pb" => Ok(Self::PetaByte),
174+
"e" | "eb" => Ok(Self::ExaByte),
170175
// power of twos
171176
"ki" | "kib" => Ok(Self::KibiByte),
172177
"mi" | "mib" => Ok(Self::MebiByte),
173178
"gi" | "gib" => Ok(Self::GibiByte),
174179
"ti" | "tib" => Ok(Self::TebiByte),
175180
"pi" | "pib" => Ok(Self::PebiByte),
181+
"ei" | "eib" => Ok(Self::ExbiByte),
176182
_ => Err(format!("couldn't parse unit of {unit:?}")),
177183
}
178184
}

0 commit comments

Comments
 (0)