Crates.io | gabi |
lib.rs | gabi |
version | 0.2.6 |
source | src |
created_at | 2022-01-09 13:19:34.193872 |
updated_at | 2022-02-14 20:44:17.304765 |
description | This crate provides a simple `Bytes` struct that stores a number and displays it in human readable format |
homepage | |
repository | https://gitlab.com/jeancf/gabi |
max_upload_size | |
id | 510802 |
size | 3,476,938 |
This crate provides a mechanism to store numbers and display them as a multiple of the chosen power of 1000 bytes or 1024 bytes, e.g. in Megabytes or Mebibytes for human readability.
Example:
let mut bb = BytesConfig::default();
let b = bb.bytes(5247 as u16);
println!("{}", b); // Prints "5.25 KB"
The number is stored internally in the same type as was provided to initialize the struct: u16
in this example.
The Bytes
structs are displayed using the preferences held in the BytesConfig
struct that created them:
bb.set_precision(1);
println!("{}", b); // Prints "5.3 KB"
See example for more details.
Writing this simple crate was a good exercise to learn generics, trait bounds and automated tests.
Decimal | Name |
---|---|
1 | Byte |
1000 | Kilo |
1000^2 | Mega |
1000^3 | Giga |
1000^4 | Tera |
1000^5 | Peta |
1000^6 | Exa |
1000^7 | Zetta |
1000^8 | Yotta |
Decimal | Name |
---|---|
1 | Byte |
1024 | Kibi |
1024^2 | Mebi |
1024^3 | Gibi |
1024^4 | Tebi |
1024^5 | Pebi |
1024^6 | Exbi |
1024^7 | Zebi |
1024^8 | Yobi |
Ze* and Yo* are outside the range of numbers that can be represented by u64. The maximum value is 1.84467440737e+19 or 18.4467440737*1000^6 which corresponds to 18.45 Exabytes (and 16.00 Exbibytes).
By default or through:
bb.set_aligned(false);
Will print:
99 B
999.22 KB
999 B
999.22 KiB
Through:
bb.set_aligned(true);
Will print:
99 B
999.22 KB
99 B
999.22 KiB
not implemented
99 B
999.22 KB
99 B
999.22 KiB
not implemented
99 B
999.22 KB
99 B
999.22 KiB
not implemented
99 B
999.22 KB
99 B
999.22 KiB