{"id":4460,"date":"2018-08-11T15:56:39","date_gmt":"2018-08-11T20:56:39","guid":{"rendered":"http:\/\/neosmart.net\/blog\/?p=4460"},"modified":"2018-08-11T15:56:39","modified_gmt":"2018-08-11T20:56:39","slug":"prettysize-for-rust","status":"publish","type":"post","link":"https:\/\/neosmart.net\/blog\/prettysize-for-rust\/","title":{"rendered":"PrettySize for rust"},"content":{"rendered":"<p><a href=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size.png\" rel=\"follow\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4074 size-thumbnail alignright colorbox-4460\" src=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-150x150.png\" alt=\"\" width=\"150\" height=\"150\" srcset=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-150x150.png 150w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-600x600.png 600w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-1024x1024.png 1024w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-300x300.png 300w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size.png 1200w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a>We&#8217;ve just published a rust port of our PrettySize.NET library, now available <a href=\"https:\/\/crates.io\/crates\/size\" rel=\"nofollow\">via cargo<\/a> and <a href=\"https:\/\/github.com\/neosmart\/prettysize-rs\" rel=\"nofollow\">github<\/a>. Like its .NET predecessor, PrettySize-rs aims to provide a comprehensive API for dealing with file sizes, covering both manipulation and human-readable formatting.<\/p>\n<p><!--more--><\/p>\n<p>Before anything else, here&#8217;s a quick example:<\/p>\n<pre><code class=\"language-rust\">let bytes = 42 * size::MiB;\r\nassert_eq!(bytes, 44040192);\r\n\r\nlet bytes = Size::MiB(42);\r\nprintln!(\"{}\", bytes); \/\/ prints \"42.0 MiB\"\r\n<\/code><\/pre>\n<p>PrettySize 0.1 currently provides the following, which are pretty much essentials when it comes to interacting with file sizes:<\/p>\n<ul>\n<li>Defines for base-two (KiB, MiB, &#8230;) and base-ten (MB, GB, &#8230;) constants in the <code>size<\/code>\u00a0namespace,<\/li>\n<li>A zero-cost, arbitrary-precision type to store file sizes (<code>size::Size<\/code>) annotated with their unit type,<sup id=\"rf1-4460\"><a href=\"#fn1-4460\" title=\"Here, arbitrary precision means that it&rsquo;s actually size::Size&lt;T&gt;&nbsp;where T&nbsp;can be any numeric type.\" rel=\"footnote\">1<\/a><\/sup><\/li>\n<li>A <code>std::fmt::Display<\/code>\u00a0impl for <code>Size<\/code>\u00a0that will emit human-readable values for file sizes (as shown above),<\/li>\n<li>A <code>Size::format(...)<\/code>\u00a0function 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,<\/li>\n<li>A <code>Size::bytes()<\/code>\u00a0function to retrieve the equivalent byte count for any <code>Size<\/code><\/li>\n<\/ul>\n<p>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.<\/p>\n<p>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!<\/p>\n<p class=\"save\"><a href=\"https:\/\/github.com\/neosmart\/prettysize-rs\" rel=\"nofollow\">Download PrettySize<\/a><\/p>\n<p align=\"right\">[<a href=\"https:\/\/github.com\/neosmart\/prettysize-rs\/blob\/master\/README.md\" rel=\"nofollow\">readme<\/a>] [<a href=\"https:\/\/docs.rs\/crate\/size\/0.1\" rel=\"nofollow\">docs<\/a>]<\/p>\n<hr class=\"footnotes\"><ol class=\"footnotes\"><li id=\"fn1-4460\"><p>Here, arbitrary precision means that it&#8217;s actually <code>size::Size&lt;T&gt;<\/code>\u00a0where <code>T<\/code>\u00a0can be any numeric type.&nbsp;<a href=\"#rf1-4460\" class=\"backlink\" title=\"Jump back to footnote 1 in the text.\">&#8617;<\/a><\/p><\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>We&#8217;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.<\/p>\n","protected":false},"author":505,"featured_media":4074,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[52,964,11,936],"class_list":["post-4460","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-open-source","tag-prettysize","tag-programming","tag-rust"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size.png","jetpack_shortlink":"https:\/\/wp.me\/p4xDa-19W","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/users\/505"}],"replies":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/comments?post=4460"}],"version-history":[{"count":1,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4460\/revisions"}],"predecessor-version":[{"id":4461,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4460\/revisions\/4461"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media\/4074"}],"wp:attachment":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media?parent=4460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/categories?post=4460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/tags?post=4460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}