Crates.io | bytescale |
lib.rs | bytescale |
version | |
source | src |
created_at | 2025-02-05 06:06:27.273025+00 |
updated_at | 2025-02-13 17:01:59.104259+00 |
description | Demonstration of humanbyte usage for human readable byte functions |
homepage | |
repository | https://github.com/lthiery/humanbyte |
max_upload_size | |
id | 1543607 |
Cargo.toml error: | TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
ByteScale
is a utility for human-readable byte count representations. It is derived using the HumanByte
procedural macro and serves as an example for HumanByte.
Features:
ByteScale
type which presents size units convertible to different size units.ByteScale
.ByteScale
, allowing to parse from string size representations like 1.5KiB and 521TiB.use bytescale::ByteScale;
macro_rules! assert_display {
($expected:expr, $bytescale:expr) => {
assert_eq!($expected, format!("{}", $bytescale));
};
}
fn test_display() {
assert_display!("215 B", ByteScale::b(215));
assert_display!("1.0 KiB", ByteScale::kib(1));
assert_display!("301.0 KiB", ByteScale::kib(301));
assert_display!("419.0 MiB", ByteScale::mib(419));
assert_display!("518.0 GiB", ByteScale::gib(518));
assert_display!("815.0 TiB", ByteScale::tib(815));
assert_display!("609.0 PiB", ByteScale::pib(609));
}
fn test_display_alignment() {
assert_eq!("|357 B |", format!("|{:10}|", ByteScale(357)));
assert_eq!("| 357 B|", format!("|{:>10}|", ByteScale(357)));
assert_eq!("|357 B |", format!("|{:<10}|", ByteScale(357)));
assert_eq!("| 357 B |", format!("|{:^10}|", ByteScale(357)));
assert_eq!("|-----357 B|", format!("|{:->10}|", ByteScale(357)));
assert_eq!("|357 B-----|", format!("|{:-<10}|", ByteScale(357)));
assert_eq!("|--357 B---|", format!("|{:-^10}|", ByteScale(357)));
}
macro_rules! assert_to_string {
($expected:expr, $actual:expr, $si:expr) => {
assert_eq!($expected.to_string(), $actual.to_string_as($si));
};
}
fn test_to_string_as() {
use humanbyte::Format;
assert_to_string!("215 B", ByteScale::b(215), Format::IEC);
assert_to_string!("215 B", ByteScale::b(215), Format::SI);
assert_to_string!("1.0 KiB", ByteScale::kib(1), Format::IEC);
assert_to_string!("1.0 kB", ByteScale::kib(1), Format::SI);
assert_to_string!("293.9 KiB", ByteScale::kb(301), Format::IEC);
assert_to_string!("301.0 kB", ByteScale::kb(301), Format::SI);
assert_to_string!("1.0 MiB", ByteScale::mib(1), Format::IEC);
assert_to_string!("1.0 MB", ByteScale::mib(1), Format::SI);
assert_to_string!("1.9 GiB", ByteScale::mib(1907), Format::IEC);
assert_to_string!("2.0 GB", ByteScale::mib(1908), Format::SI);
assert_to_string!("399.6 MiB", ByteScale::mb(419), Format::IEC);
assert_to_string!("419.0 MB", ByteScale::mb(419), Format::SI);
assert_to_string!("482.4 GiB", ByteScale::gb(518), Format::IEC);
assert_to_string!("518.0 GB", ByteScale::gb(518), Format::SI);
assert_to_string!("741.2 TiB", ByteScale::tb(815), Format::IEC);
assert_to_string!("815.0 TB", ByteScale::tb(815), Format::SI);
assert_to_string!("540.9 PiB", ByteScale::pb(609), Format::IEC);
assert_to_string!("609.0 PB", ByteScale::pb(609), Format::SI);
}
use bytescale::ByteScale;
fn byte_arithmetic_operator() {
let x = ByteScale::mb(1);
let y = ByteScale::kb(100);
let plus = x + y;
print!("{}", plus);
let minus = ByteScale::tb(100) + ByteScale::gb(4);
print!("{}", minus);
}