human-units
Size, duration and other SI units serialization and formatting library designed for configuration files and command line arguments.
Introduction
human-units is a library with Size, Duration and other SI-related types specifically designed to be used in configuration files and as command line arguments.
These types serialize sizes and durations in exact but human-readable form.
The library also provides FormatSize, FormatDuration traits
to print approximate values in a short human-readable form.
- No floating point operations.
- No dependencies by default.
- Supports serde.
- Supports clap.
- Supports
no_std. - Tested with Miri.
- 72–85% faster than similar libraries (see benchmarks below).
- 50–87% less binary size compared to similar libraries (see benchmarks below).
Examples
Exact human-readable size/duration
use ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Inexact short human-readable size/duration
use Duration;
use ;
assert_eq!;
assert_eq!;
Custom output
use Colorize;
use Duration;
use ;
/// Prints the unit in cyan.
;
// prints "1 m ago", "m" is printed with cyan color
println!;
Serde integration
use Size;
use Serialize;
let object = SizeWrapper;
assert_eq!;
Clap integration
use Parser;
use ;
let args = parse_from;
assert_eq!;
assert_eq!;
SI units
use ;
// Convert from hertz, internal representation is nHz (nanohertz).
let cpu_freq = with_si_prefix;
assert_eq!;
assert_eq!;
IEC units
use ;
let size = with_iec_prefix;
assert_eq!;
assert_eq!;
Custom units
use si_unit;
use iec_unit;
;
let volume = Volume;
assert_eq!;
;
let throughput = Throughput;
assert_eq!;
Performance benchmarks
Benchmarks were done with Rust 1.80.1 on a x86_64 laptop.
Format size
| Library | Version | Features | Benchmark | Time |
|---|---|---|---|---|
human_bytes |
0.4.3 | fast |
format_size_then_to_string |
88.40 ns ± 5.02 ns |
human-repr |
1.1.0 | 1024,space |
format_size_then_to_string |
161.38 ns ± 13.29 ns |
human-units |
0.1.3 | format_size_then_to_string |
24.24 ns ± 1.23 ns |
Format duration
| Library | Version | Features | Benchmark | Time |
|---|---|---|---|---|
human-repr |
1.1.0 | 1024,space |
format_duration_then_to_string |
229.47 ns ± 11.90 ns |
human-units |
0.1.3 | format_duration_then_to_string |
41.55 ns ± 2.77 ns |
Executable size benchmarks
Benchmarks were done with Rust 1.80.1 on a x86_64 laptop.
Format size
| Library | Version | Features | Benchmark | Executable size, B |
|---|---|---|---|---|
human_bytes |
0.4.3 | fast |
print formatted size | 8192 |
human-repr |
1.1.0 | 1024,space |
print formatted size | 28672 |
human-units |
0.1.3 | print formatted size | 4096 |
Format duration
| Library | Version | Features | Benchmark | Executable size, B |
|---|---|---|---|---|
human-repr |
1.1.0 | 1024,space |
print formatted duration | 28672 |
human-units |
0.1.3 | print formatted duration | 4096 |