20 releases (9 stable)

2.0.1 Feb 28, 2025
1.3.3 Mar 31, 2025
1.3.0 Aug 23, 2023
1.2.0 Feb 24, 2023
0.0.1 Apr 19, 2015

#3 in Value formatting

Download history 138228/week @ 2024-12-21 154142/week @ 2024-12-28 286654/week @ 2025-01-04 322050/week @ 2025-01-11 281184/week @ 2025-01-18 308161/week @ 2025-01-25 352979/week @ 2025-02-01 371186/week @ 2025-02-08 334635/week @ 2025-02-15 371429/week @ 2025-02-22 375245/week @ 2025-03-01 396370/week @ 2025-03-08 429181/week @ 2025-03-15 393399/week @ 2025-03-22 560781/week @ 2025-03-29 650146/week @ 2025-04-05

2,098,212 downloads per month
Used in 867 crates (275 directly)

Apache-2.0

38KB
892 lines

ByteSize

crates.io Documentation Version Apache 2.0 licensed
Dependency Status Download

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!("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());

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

Dependencies

~180KB