mirror of
https://github.com/LeopoldArkham/humansize
synced 2024-11-10 06:14:15 +00:00
update docs
This commit is contained in:
parent
38943dc0b1
commit
009ed6ba5d
2 changed files with 27 additions and 14 deletions
26
README.md
26
README.md
|
@ -2,7 +2,6 @@
|
|||
|
||||
[Documentation](https://docs.rs/humansize/latest/humansize/)
|
||||
|
||||
|
||||
## Features
|
||||
Humansize is a humanization library for information size that is:
|
||||
- Simple & convenient to use
|
||||
|
@ -27,7 +26,7 @@ humansize = "2.0.0"
|
|||
- `DECIMAL` (SI)
|
||||
- `BINARY` (IEC)
|
||||
- `WINDOWS` (IEC values but SI units)
|
||||
2. Call `format_size` with an unsigned integer
|
||||
2. Call `format_size` with an unsigned integer
|
||||
|
||||
```rust
|
||||
use humansize::{format_size, DECIMAL};
|
||||
|
@ -60,12 +59,12 @@ Specify the `no_alloc` feature flag in your project's `cargo.toml`:
|
|||
...
|
||||
humansize = { version = "2.0.0", features = ["no_alloc"] }
|
||||
```
|
||||
This excludes all allocating code from compilation. You may now use the library's internal `Formatter` struct, which implements `core::fmt::display` so that you can `write!` it to a custom buffer of your choice:
|
||||
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:
|
||||
```rust
|
||||
use humansize::{Formatter, DECIMAL};
|
||||
use humansize::{SizeFormatter, DECIMAL};
|
||||
|
||||
let formatter = Formatter::new(1_000_000, DECIMAL);
|
||||
assert_eq!(format!({}, formatter), '1 MB');
|
||||
let formatter = SizeFormatter::new(1_000_000usize, DECIMAL);
|
||||
assert_eq!(format!("{}", formatter), "1 MB");
|
||||
```
|
||||
### ... with the `impl` style API:
|
||||
For stylistic reasons, you may prefer to use the impl-style API of earlier versions of the crate.
|
||||
|
@ -81,11 +80,11 @@ Enabling this feature makes two methods available:
|
|||
- `format_size_i` on signed integer types.
|
||||
|
||||
To use it, bring the FormatSize trait into scope and call its method on an integer type:
|
||||
```rust
|
||||
use humansize::{FormatSize, Decimal};
|
||||
```ignore
|
||||
use humansize::{FormatSize, FormatSizeI DECIMAL};
|
||||
|
||||
assert_eq!(1_000_000u64.format_size(DECIMAL), "1 MB");
|
||||
assert_eq!(-1_000_000).format_size_i(DECIMAL), "-1 MB");
|
||||
assert_eq!((-1_000_000).format_size_i(DECIMAL), "-1 MB");
|
||||
```
|
||||
### ... to further customize the output:
|
||||
Humansize exports three default option sets:
|
||||
|
@ -103,13 +102,20 @@ The solutions presented above only accept unsigned integer types as input (`usiz
|
|||
- `FormatSize` trait : `FormatSizeI` trait
|
||||
- `SizeFormatter` : `ISizeFormatter`
|
||||
```rust
|
||||
use humansize::{format_size_i, make_format_i, DECIMAL};
|
||||
use humansize::{format_size_i, make_format_i, ISizeFormatter, DECIMAL};
|
||||
|
||||
assert_eq!(&format_size_i(-1_000_000, DECIMAL), "-1 MB");
|
||||
|
||||
let signed_formatter = make_format_i(DECIMAL);
|
||||
assert_eq!(&signed_formatter(-1_000_000), "-1 MB");
|
||||
|
||||
// With the `impl-style` feature enabled:
|
||||
// use humansize::FormatSizeI;
|
||||
// assert_eq(-1_000_000.format_size(DECIMAL), "-1 MB");
|
||||
|
||||
let signed_size_formatter = ISizeFormatter::new(-1_000_000, DECIMAL);
|
||||
assert_eq!(format!("{}", signed_size_formatter), "-1 MB");
|
||||
|
||||
```
|
||||
|
||||
## License
|
||||
|
|
15
src/lib.rs
15
src/lib.rs
|
@ -59,11 +59,11 @@ Specify the `no_alloc` feature flag in your project's `cargo.toml`:
|
|||
...
|
||||
humansize = { version = "2.0.0", features = ["no_alloc"] }
|
||||
```
|
||||
This excludes all allocating code from compilation. You may now use the library's internal `Formatter` struct, which implements `core::fmt::display` so that you can `write!` it to a custom buffer of your choice:
|
||||
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:
|
||||
```rust
|
||||
use humansize::{ISizeFormatter, DECIMAL};
|
||||
use humansize::{SizeFormatter, DECIMAL};
|
||||
|
||||
let formatter = ISizeFormatter::new(1_000_000, DECIMAL);
|
||||
let formatter = SizeFormatter::new(1_000_000usize, DECIMAL);
|
||||
assert_eq!(format!("{}", formatter), "1 MB");
|
||||
```
|
||||
### ... with the `impl` style API:
|
||||
|
@ -102,13 +102,20 @@ The solutions presented above only accept unsigned integer types as input (`usiz
|
|||
- `FormatSize` trait : `FormatSizeI` trait
|
||||
- `SizeFormatter` : `ISizeFormatter`
|
||||
```rust
|
||||
use humansize::{format_size_i, make_format_i, DECIMAL};
|
||||
use humansize::{format_size_i, make_format_i, ISizeFormatter, DECIMAL};
|
||||
|
||||
assert_eq!(&format_size_i(-1_000_000, DECIMAL), "-1 MB");
|
||||
|
||||
let signed_formatter = make_format_i(DECIMAL);
|
||||
assert_eq!(&signed_formatter(-1_000_000), "-1 MB");
|
||||
|
||||
// With the `impl-style` feature enabled:
|
||||
// use humansize::FormatSizeI;
|
||||
// assert_eq(-1_000_000.format_size(DECIMAL), "-1 MB");
|
||||
|
||||
let signed_size_formatter = ISizeFormatter::new(-1_000_000, DECIMAL);
|
||||
assert_eq!(format!("{}", signed_size_formatter), "-1 MB");
|
||||
|
||||
```
|
||||
*/
|
||||
|
||||
|
|
Loading…
Reference in a new issue