A simple, complete, const
-everything, saturating, human-friendly, #![no_std]
Rust library for byte units.
```rust use ubyte::{ByteUnit, ToByteUnit};
// Constructors and associated units for all SI units up to exbibyte. let halfmb = 500.kilobytes(); let halfmb = ByteUnit::Kilobyte(500); let half_mb = 500 * ByteUnit::kB;
// All arithmetic and conversions are saturating. let exbibytetoolargea = 1024 * ByteUnit::EiB; let exbibytetoolargeb = ByteUnit::Exbibyte(1024); let exbibytetoolargec = 1024.exbibytes(); asserteq!(exbibytetoolargea, ByteUnit::maxvalue()); asserteq!(exbibytetoolargeb, ByteUnit::maxvalue()); asserteq!(exbibytetoolargec, ByteUnit::maxvalue());
// Printing is human-friendly and customizeable. asserteq!(323.kilobytes().tostring(), "323kB"); asserteq!(3.mebibytes().tostring(), "3MiB"); asserteq!((7.gigabytes() + 58.mebibytes() + 3.kilobytes()).tostring(), "7.06GB"); asserteq!(format!("{:.0}", 7.gibibytes() + 920.mebibytes()), "8GiB"); asserteq!(format!("{:.3}", 7.gibibytes() + 920.mebibytes()), "7.898GiB"); asserteq!(format!("{:04.2}", 999.kilobytes() + 990.bytes()), "0976.55KiB"); asserteq!(format!("{:02.0}", 999.kilobytes() + 990.bytes()), "01MB");
// Parsing is intuitive. asserteq!("10 KiB".parse().unwrap(), 10.kibibytes()); asserteq!("10kb".parse().unwrap(), 10.kilobytes()); asserteq!("512Kb".parse().unwrap(), 512.kilobytes()); asserteq!("0.2MB".parse().unwrap(), 200.kilobytes()); asserteq!("1.5 MiB".parse().unwrap(), 1.mebibytes() + 512.kibibytes()); asserteq!("7.25 gb".parse().unwrap(), 7.gigabytes() + 250.megabytes()); ```
See the documentation for detailed usage information.
Add the following to your Cargo.toml
:
toml
[dependencies]
ubyte = "0.10"
For serde
(de)serialization support, enable the serde
feature, which is
disabled by default:
toml
[dependencies]
ubyte = { version = "0.10", features = ["serde"] }
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.