Skip to main content

Crate bytesize

Crate bytesize 

Source
Expand description

ByteSize is a semantic wrapper for byte count representations.

Features:

  • Pre-defined constants for various size units (e.g., B, KB, KiB, MB, MiB, … EB, EiB).
  • 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” and “521TiB”.
  • Serde support for binary and human-readable deserializers like JSON.

§Feature flags

  • std (default): Enables the alloc feature and standard library optimizations.
  • alloc: Enables parsing and formatting.
  • arbitrary: Implements arbitrary::Arbitrary for ByteSize.
  • serde: Enables alloc and implements serialization and deserialization for ByteSize.

Disable the default features to use only the core data types, conversions, and constants.

§Examples

Construction using SI or IEC helpers.

use bytesize::ByteSize;

assert!(ByteSize::kib(4) > ByteSize::kb(4));

Display as human-readable string.

use bytesize::ByteSize;

assert_eq!("518.0 GiB", ByteSize::gib(518).display().iec().to_string());
assert_eq!("556.2 GB", ByteSize::gib(518).display().si().to_string());
assert_eq!("518.0G", ByteSize::gib(518).display().iec_short().to_string());
assert_eq!("4.0 Kib", ByteSize::b(512).display().iec_bits().to_string());
assert_eq!("4.1 kb", ByteSize::b(512).display().si_bits().to_string());

Arithmetic operations are supported.

use bytesize::ByteSize;

let plus = ByteSize::mb(1) + ByteSize::kb(100);
println!("{plus}");

let minus = ByteSize::tb(1) - ByteSize::gb(4);
assert_eq!(ByteSize::gb(996), minus);

Structs§

ByteSize
Byte size representation.
Display
Formatting display wrapper for ByteSize.
UnitParseError
Error returned when parsing a Unit fails.

Enums§

Unit
Scale unit.

Constants§

EB
Number of bytes in 1 exabyte.
EIB
Number of bytes in 1 exbibyte.
GB
Number of bytes in 1 gigabyte.
GIB
Number of bytes in 1 gibibyte.
KB
Number of bytes in 1 kilobyte.
KIB
Number of bytes in 1 kibibyte.
MB
Number of bytes in 1 megabyte.
MIB
Number of bytes in 1 mebibyte.
PB
Number of bytes in 1 petabyte.
PIB
Number of bytes in 1 pebibyte.
TB
Number of bytes in 1 terabyte.
TIB
Number of bytes in 1 tebibyte.

Functions§

eb
Converts a quantity of exabytes to bytes.
eib
Converts a quantity of exbibytes to bytes.
gb
Converts a quantity of gigabytes to bytes.
gib
Converts a quantity of gibibytes to bytes.
kb
Converts a quantity of kilobytes to bytes.
kib
Converts a quantity of kibibytes to bytes.
mb
Converts a quantity of megabytes to bytes.
mib
Converts a quantity of mebibytes to bytes.
pb
Converts a quantity of petabytes to bytes.
pib
Converts a quantity of pebibytes to bytes.
tb
Converts a quantity of terabytes to bytes.
tib
Converts a quantity of tebibytes to bytes.