Skip to content

An utility that easily makes bytes size representation and helps its arithmetic operations.

License

Notifications You must be signed in to change notification settings

bytesize-rs/bytesize

Repository files navigation

ByteSize

CI Crates.io Version

ByteSize is a semantic wrapper for byte count representations.

Features:

  • Pre-defined constants for various size units (e.g., B, Kb, Kib, Mb, Mib, Gb, Gib, ... PB).
  • 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.

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!("482.4 GiB", ByteSize::gb(518).to_string_as(true));
assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(false));

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);

About

An utility that easily makes bytes size representation and helps its arithmetic operations.

Resources

License

Stars

Watchers

Forks

Packages

No packages published