Typed Bytesize
The library provides two types to represent bytesize:
- ByteSizeSi for decimal prefix unit (SI)
- ByteSizeIec for binary prefix unit (IEC 80000-13)
Functions
- Bytesize types can parse each other's units (e.g.
ByteSizeIec
can parse SI values like114514GB
); - Bytesize values will only be formatted as the unit has their owned prefix;
- Bytesize types can be converted to each other;
- Supporting addition, subtraction, scalar multiplication arithmetic operations;
- Optional serde support.
Example
use ;
// SI
assert_eq!;
assert_eq!;
print!; // 310.0kB
// IEC
assert_eq!;
assert_eq!;
print!; // 91.5GiB
For more detailed examples, refer to tests.
BNF
Parsing follows the rule:
expr ::= num | term
term ::= decimal " "* unit
decimal ::= num | float
float ::= num "." num
num ::= [0-9]+
Features
serde
: enable serde onByteSizeSi
andByteSizeIec
.u128
: useu128
instead ofu64
as inner numeric type to support larger units. (TODO)