#byte #units #si-units #conversion #integer #unit-conversion #human

no-std ubyte

A simple, complete, const-everything, saturating, human-friendly, no_std library for byte units

4 releases

0.10.4 Oct 2, 2023
0.10.3 Sep 4, 2022
0.10.2 May 17, 2022
0.10.1 Nov 2, 2020
0.9.1 Aug 5, 2020

#18 in Value formatting

Download history 28173/week @ 2024-01-02 29098/week @ 2024-01-09 36106/week @ 2024-01-16 34289/week @ 2024-01-23 36215/week @ 2024-01-30 35818/week @ 2024-02-06 33512/week @ 2024-02-13 35343/week @ 2024-02-20 35166/week @ 2024-02-27 37762/week @ 2024-03-05 35277/week @ 2024-03-12 37817/week @ 2024-03-19 30689/week @ 2024-03-26 36120/week @ 2024-04-02 32436/week @ 2024-04-09 28898/week @ 2024-04-16

134,622 downloads per month
Used in 343 crates (12 directly)

MIT/Apache

40KB
648 lines

ubyte   crates.io docs.rs

A simple, complete, const-everything, saturating, human-friendly, #![no_std] Rust library for byte units.

use ubyte::{ByteUnit, ToByteUnit};

// Constructors and associated units for all SI units up to exbibyte.
let half_mb = 500.kilobytes();
let half_mb = ByteUnit::Kilobyte(500);
let half_mb = 500 * ByteUnit::kB;

// All arithmetic and conversions are saturating.
let exbibyte_too_large_a = 1024 * ByteUnit::EiB;
let exbibyte_too_large_b = ByteUnit::Exbibyte(1024);
let exbibyte_too_large_c = 1024.exbibytes();
assert_eq!(exbibyte_too_large_a, ByteUnit::max_value());
assert_eq!(exbibyte_too_large_b, ByteUnit::max_value());
assert_eq!(exbibyte_too_large_c, ByteUnit::max_value());

// Printing is human-friendly and customizeable.
assert_eq!(323.kilobytes().to_string(), "323kB");
assert_eq!(3.mebibytes().to_string(), "3MiB");
assert_eq!((7.gigabytes() + 58.mebibytes() + 3.kilobytes()).to_string(), "7.06GB");
assert_eq!(format!("{:.0}", 7.gibibytes() + 920.mebibytes()), "8GiB");
assert_eq!(format!("{:.3}", 7.gibibytes() + 920.mebibytes()), "7.898GiB");
assert_eq!(format!("{:04.2}", 999.kilobytes() + 990.bytes()), "0976.55KiB");
assert_eq!(format!("{:02.0}", 999.kilobytes() + 990.bytes()), "01MB");

// Parsing is intuitive.
assert_eq!("10 KiB".parse().unwrap(), 10.kibibytes());
assert_eq!("10kb".parse().unwrap(), 10.kilobytes());
assert_eq!("512Kb".parse().unwrap(), 512.kilobytes());
assert_eq!("0.2MB".parse().unwrap(), 200.kilobytes());
assert_eq!("1.5 MiB".parse().unwrap(), 1.mebibytes() + 512.kibibytes());
assert_eq!("7.25 gb".parse().unwrap(), 7.gigabytes() + 250.megabytes());

See the documentation for detailed usage information.

Usage

Add the following to your Cargo.toml:

[dependencies]
ubyte = "0.10"

For serde (de)serialization support, enable the serde feature, which is disabled by default:

[dependencies]
ubyte = { version = "0.10", features = ["serde"] }

License

ubyte is licensed under either of the following, at your option:

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in ubyte by you shall be dual licensed as above without any additional terms or conditions.

Dependencies

~175KB