#byte-size #size #byte #units #proc-macro #unit

macro human-bytesize-procmacro

A procedural macro for parsing and computing human-readable byte sizes with support for various units like KB, KiB, MB, MiB, and more

1 unstable release

new 0.0.0 Jan 6, 2025

#30 in #byte-size

Download history 121/week @ 2025-01-01

121 downloads per month

MIT license

21KB
367 lines

human-bytesize-procmacro

GitHub Build docs.rs MSRV deps.rs unsafe forbidden LICENSE

A procedural macro for parsing and computing human-readable byte sizes with support for various units like KB, KiB, MB, MiB, and more.

Setup

To use this crate, add the following entry to your Cargo.toml file in the dependencies section:

[dependencies]
human-bytesize-procmacro = "0.0.0"

Alternatively, you can use the cargo add subcommand:

cargo add human-bytesize-procmacro

Usage

Use the human_bytesize macro to convert human-readable byte sizes into their byte equivalents by passing a size with its unit:

use human_bytesize_procmacro::human_bytesize;

assert_eq!(human_bytesize!(10KB), 10 * 1000);
assert_eq!(human_bytesize!(16 KiB), 16 * 1024);
let variable = 160;
assert_eq!(human_bytesize!({ variable } MB), variable * 1000 * 1000);

For more usage examples, refer to the documentation available at docs.rs.

Supported units

Unit Code Unit Name Bytes
B Byte $1000^0 = 1024^0 = 1$
K or KB Kilobyte $1000^1 = 1000$
Ki or KiB Kibibyte $1024^1 = 1024$
M or MB Megabyte $1000^2 = 1000 \cdot 1000$
Mi or MiB Mebibyte $1024^2 = 1024 \cdot 1024$
G or GB Gigabyte $1000^3 = 1000 \cdot 1000 \cdot 1000$
Gi or GiB Gibibyte $1024^3 = 1024 \cdot 1024 \cdot 1024$
T or TB Terabyte $1000^4 = 1000 \cdot 1000 \cdot 1000 \cdot 1000$
Ti or TiB Tebibyte $1024^4 = 1024 \cdot 1024 \cdot 1024 \cdot 1024$
P or PB Petabyte $1000^5 = 1000 \cdot 1000 \cdot 1000 \cdot 1000 \cdot 1000$
Pi or PiB Pebibyte $1024^5 = 1024 \cdot 1024 \cdot 1024 \cdot 1024 \cdot 1024$
E or EB Exabyte $1000^6 = 1000 \cdot 1000 \cdot 1000 \cdot 1000 \cdot 1000 \cdot 1000$
Ei or EiB Exbibyte $1024^6 = 1024 \cdot 1024 \cdot 1024 \cdot 1024 \cdot 1024 \cdot 1024$
Z or ZB Zettabyte $1000^7 = 1000 \cdot 1000 \cdot 1000 \cdot 1000 \cdot 1000 \cdot 1000 \cdot 1000$
Zi or ZiB Zebibyte $1024^7 = 1024 \cdot 1024 \cdot 1024 \cdot 1024 \cdot 1024 \cdot 1024 \cdot 1024$
Y or YB Yottabyte $1000^8 = 1000 \cdot 1000 \cdot 1000 \cdot 1000 \cdot 1000 \cdot 1000 \cdot 1000 \cdot 1000$
Yi or YiB Yobibyte $1024^8 = 1024 \cdot 1024 \cdot 1024 \cdot 1024 \cdot 1024 \cdot 1024 \cdot 1024 \cdot 1024$

License

This crate is licensed under the MIT License.

Dependencies

~1–1.7MB
~34K SLoC