We’ve just published a rust port of our PrettySize.NET library, now available via cargo and github. Like its .NET predecessor, PrettySize-rs aims to provide a comprehensive API for dealing with file sizes, covering both manipulation and human-readable formatting.
Before anything else, here’s a quick example:
let bytes = 42 * size::MiB;
assert_eq!(bytes, 44040192);
let bytes = Size::MiB(42);
println!("{}", bytes); // prints "42.0 MiB"
PrettySize 0.1 currently provides the following, which are pretty much essentials when it comes to interacting with file sizes:
- Defines for base-two (KiB, MiB, …) and base-ten (MB, GB, …) constants in the
size
namespace, - A zero-cost, arbitrary-precision type to store file sizes (
size::Size
) annotated with their unit type,1 - A
std::fmt::Display
impl forSize
that will emit human-readable values for file sizes (as shown above), - A
Size::format(...)
function that provides fine-grained control over the human-readable output, allowing you to format sizes in base-2 or base-10 units, choose between abbreviated and unabbreviated unit names, and lower vs upper case output, - A
Size::bytes()
function to retrieve the equivalent byte count for anySize
The currently provided functions should suffice for most cases involving ingestion of file sizes or outputting formatted file sizes for UI or logging purposes; but more work remains to be done.
As with all of our other contributions, PrettySize-rs has been published under the liberal terms of the MIT public license and is available for immediate (re)use or modification at GitHub. Pull requests are both welcomed and appreciated!
Here, arbitrary precision means that it’s actually
size::Size<T>
whereT
can be any numeric type. ↩