28

PrettySize for rust

 5 years ago
source link: https://www.tuicool.com/articles/hit/fMBziyV
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

E3emqmY.png!web 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,
  • A std::fmt::Display  impl for Size  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 any Size

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!

Download PrettySize

[ readme ] [ docs ]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK