Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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.
///
Expand Down Expand Up @@ -146,6 +150,16 @@ pub fn pib<V: Into<u64>>(size: V) -> u64 {
size.into() * PIB
}

/// Converts a quantity of exabytes to bytes.
pub fn eb<V: Into<u64>>(size: V) -> u64 {
size.into() * EB
}

/// Converts a quantity of exbibytes to bytes.
pub fn eib<V: Into<u64>>(size: V) -> u64 {
size.into() * EIB
}

/// Byte size representation.
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Default)]
pub struct ByteSize(pub u64);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down
6 changes: 6 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ enum Unit {
GigaByte,
TeraByte,
PetaByte,
ExaByte,
// power of twos
KibiByte,
MebiByte,
GibiByte,
TebiByte,
PebiByte,
ExbiByte,
}

impl Unit {
Expand All @@ -76,12 +78,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,
}
}
}
Expand Down Expand Up @@ -167,12 +171,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:?}")),
}
}
Expand Down
Loading