Byte Unit
A library for interaction with units of bytes.
The units are B for 1 byte, KB for 1000 bytes, MiB for 1048576 bytes, GB for 1000000000 bytes, etc, and up to E or Y (if the u128
feature is enabled).
Usage
The data types for storing the size in bits/bytes are u64
by default, meaning the highest supported unit is up to E. If the u128
feature is enabled, the data types will use u128
, increasing the highest supported unit up to Y.
Unit
The enum Unit
can be used for representing the unit of bits/bytes.
use Unit;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Byte
The Byte
struct can be used for representing a size in bytes.
The from_*
associated functions can be used to create a Byte
instance from different data types. The as_*
methods can retrieve the size as a primitive type.
use ;
assert_eq!;
assert_eq!;
You can also parse a string to create a Byte
instance.
use Byte;
assert_eq!;
A Byte
instance can be formatted to string precisely. For more detailed usage, please refer to the implementation documentation of Display::fmt
for Byte
.
use Byte;
let byte = from_u64;
assert_eq!;
assert_eq!;
assert_eq!;
Arithmetic
There are add
, subtract
, multiply
, and divide
methods.
use Byte;
let a = from_u64;
let b = from_u64;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Find Out an Appropriate Unit
The get_exact_unit
and get_recoverable_unit
methods is useful if you want to find out a unit that is appropriate for a Byte
instance.
use ;
let byte = from_u64;
assert_eq!;
assert_eq!;
assert_eq!;
AdjustedByte
The AdjustedByte
struct can be used for roughly representing a size of bytes with a unit.
To change the unit of a Byte
instance, you can use the get_adjusted_unit
method.
An AdjustedByte
instance can be formatted to string. For more detailed usage, please refer to the implementation documentation of Display::fmt
for AdjustedByte
.
use ;
let byte = parse_str.unwrap;
let adjusted_byte = byte.get_adjusted_unit;
assert_eq!;
assert_eq!;
The get_appropriate_unit
method can be used to automatically find an appropriate unit for creating an AdjustedByte
instance.
use ;
let byte = from_u64;
let adjusted_byte = byte.get_appropriate_unit;
assert_eq!;
Bit
The Bit
struct can be used for representing a size in bits.
The bit
feature must be enabled.
Usage of the Bit
struct and the Byte
struct is very similar. Also, There is the AdjustedBit
struct. The difference lies in the fact that the parse_str
method of the Bit
struct cannot be configured to ignore case; it always does not ignore case.
use ;
let byte = parse_str.unwrap;
let adjusted_byte = byte.get_adjusted_unit;
assert_eq!;
assert_eq!;
No Std
Disable the default features to compile this crate without std.
[]
= "*"
= false
= ["byte"]
Serde Support
Enable the serde
feature to support the serde framework.
[]
= "*"
= ["serde"]
Crates.io
https://crates.io/crates/byte-unit