| 1 | # **Humansize**  [](https://gist.github.com/cheerfulstoic/d107229326a01ff0f333a1d3476e068d) |
| 2 | |
| 3 | [Documentation](https://docs.rs/humansize/latest/humansize/) |
| 4 | |
| 5 | ## Features |
| 6 | Humansize is a humanization library for information size that is: |
| 7 | - Simple & convenient to use |
| 8 | - Customizable |
| 9 | - Supports byte or bit sizes |
| 10 | - `no-std` |
| 11 | - Optionally non-allocating |
| 12 | - Optionally accepts signed values |
| 13 | |
| 14 | ## How to use it... |
| 15 | |
| 16 | Add humansize as a dependency to your project's `cargo.toml`: |
| 17 | ```toml |
| 18 | [dependencies] |
| 19 | ... |
| 20 | humansize = "2.1.3" |
| 21 | ``` |
| 22 | |
| 23 | ### ... to easily format a size: |
| 24 | |
| 25 | 1. Import the `format_size` function as well as your preferred set of defaults: |
| 26 | - `DECIMAL` (SI) |
| 27 | - `BINARY` (IEC) |
| 28 | - `WINDOWS` (IEC values but SI units) |
| 29 | 2. Call `format_size` with an unsigned integer |
| 30 | |
| 31 | ```rust |
| 32 | use humansize::{format_size, DECIMAL}; |
| 33 | |
| 34 | let size = 1_000_000u64; |
| 35 | let res: String = format_size(size, DECIMAL); |
| 36 | |
| 37 | assert_eq!(&res, "1 MB"); |
| 38 | |
| 39 | ``` |
| 40 | |
| 41 | ### ... to format many sizes: |
| 42 | To improve reusability, you can use `create_format`, which returns a formatter function akin to `format_size` but with the options argument curried so it doesn't need to be specified again: |
| 43 | |
| 44 | ```rust |
| 45 | use humansize::{make_format, DECIMAL}; |
| 46 | |
| 47 | let formatter = make_format(DECIMAL); |
| 48 | |
| 49 | assert_eq!(formatter(1_000_000u64), "1 MB"); |
| 50 | assert_eq!(formatter(1_000_000_000u64), "1 GB"); |
| 51 | //... |
| 52 | |
| 53 | ``` |
| 54 | |
| 55 | ### ... to avoid allocation: |
| 56 | Specify the `no_alloc` feature flag in your project's `cargo.toml`: |
| 57 | ```toml |
| 58 | [dependencies] |
| 59 | ... |
| 60 | humansize = { version = "2.0.0", features = ["no_alloc"] } |
| 61 | ``` |
| 62 | This excludes all allocating code from compilation. You may now use the library's internal `SizeFormatter` struct, which implements `core::fmt::display` so that you can `write!` it to a custom buffer of your choice: |
| 63 | ```rust |
| 64 | use humansize::{SizeFormatter, DECIMAL}; |
| 65 | |
| 66 | let formatter = SizeFormatter::new(1_000_000usize, DECIMAL); |
| 67 | assert_eq!(format!("{}", formatter), "1 MB"); |
| 68 | ``` |
| 69 | ### ... with the `impl` style API: |
| 70 | For stylistic reasons, you may prefer to use the impl-style API of earlier versions of the crate. |
| 71 | To do so, specify the `impl-style` feature flag in your project's `cargo.toml`: |
| 72 | |
| 73 | ```toml |
| 74 | [dependencies] |
| 75 | ... |
| 76 | humansize = { version = "2.0.0", features = ["impl_style"] } |
| 77 | ``` |
| 78 | Enabling this feature makes two methods available: |
| 79 | - `format_size` on unsigned integers types |
| 80 | - `format_size_i` on signed integer types. |
| 81 | |
| 82 | To use it, bring the FormatSize trait into scope and call its method on an integer type: |
| 83 | ```ignore |
| 84 | use humansize::{FormatSize, FormatSizeI DECIMAL}; |
| 85 | |
| 86 | assert_eq!(1_000_000u64.format_size(DECIMAL), "1 MB"); |
| 87 | assert_eq!((-1_000_000).format_size_i(DECIMAL), "-1 MB"); |
| 88 | ``` |
| 89 | ### ... to further customize the output: |
| 90 | Humansize exports three default option sets: |
| 91 | * `Decimal`: kilo = 1000, unit format is `XB`. |
| 92 | * `Binary`: kilo = 1024, unit format is `XiB`. |
| 93 | * `WINDOWS` (Windows): kilo = 1024, unit format is `XB`. |
| 94 | |
| 95 | The formatting can be further customized by providing providing your own option set. See the documentation of the `FormatSizeOptions` struct to see all the addressable parameters, and [this example](examples/custom_options.rs) for its usage. |
| 96 | |
| 97 | ### ... to accept negative values: |
| 98 | The solutions presented above only accept unsigned integer types as input (`usize`, `8`, `u16`, `u32` and `u64`). If however accepting negative values is correct for your application, a signed alternative exists for each of them that will accept signed integer types, and format them accordingly if negative: |
| 99 | |
| 100 | - `format_size` : `format_size_i` |
| 101 | - `create_format` : `create_format_i` |
| 102 | - `FormatSize` trait : `FormatSizeI` trait |
| 103 | - `SizeFormatter` : `ISizeFormatter` |
| 104 | ```rust |
| 105 | use humansize::{format_size_i, make_format_i, ISizeFormatter, DECIMAL}; |
| 106 | |
| 107 | assert_eq!(&format_size_i(-1_000_000, DECIMAL), "-1 MB"); |
| 108 | |
| 109 | let signed_formatter = make_format_i(DECIMAL); |
| 110 | assert_eq!(&signed_formatter(-1_000_000), "-1 MB"); |
| 111 | |
| 112 | // With the `impl-style` feature enabled: |
| 113 | // use humansize::FormatSizeI; |
| 114 | // assert_eq(-1_000_000.format_size(DECIMAL), "-1 MB"); |
| 115 | |
| 116 | let signed_size_formatter = ISizeFormatter::new(-1_000_000, DECIMAL); |
| 117 | assert_eq!(format!("{}", signed_size_formatter), "-1 MB"); |
| 118 | |
| 119 | ``` |
| 120 | |
| 121 | ## License |
| 122 | |
| 123 | This project is licensed under either of |
| 124 | |
| 125 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or |
| 126 | http://www.apache.org/licenses/LICENSE-2.0) |
| 127 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or |
| 128 | http://opensource.org/licenses/MIT) |
| 129 | |
| 130 | |
| 131 | ### Contribution |
| 132 | |
| 133 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 134 | for inclusion in humansize by you, as defined in the Apache-2.0 license, shall be |
| 135 | dual licensed as above, without any additional terms or conditions. |