Crate byte_unit[][src]

Byte Unit

A library for interaction with units of bytes. The units are B for 1 byte, KB for 1000 bytes, KiB for 1024 bytes, MB for 1000000 bytes, MiB for 1048576 bytes, etc, and up to ZiB which is 1180591620717411303424 bytes.

The data type for storing the size of bytes is u128 by default, but can also be changed to u64 by disabling the default features (it will also cause the highest supported unit down to PiB).

Usage

Macros

There are n_*_bytes macros can be used. The star * means the unit. For example, n_gb_bytes can be used to get a n-GB value in bytes.

#[macro_use] extern crate byte_unit;

let result = n_gb_bytes!(4);

assert_eq!(4000000000, result);

You may need to assign a primitive type if the n is not an integer.

#[macro_use] extern crate byte_unit;

let result = n_gb_bytes!(2.5, f64);

assert_eq!(2500000000, result);

Byte

The Byte structure can be used for representing a size of bytes.

The from_str associated function can parse any SIZE string and return a Byte instance in common usage. The format of a SIZE string is like “123”, “123KiB” or “50.84 MB”.

extern crate byte_unit;

use byte_unit::Byte;

let result = Byte::from_str("50.84 MB").unwrap();

assert_eq!(50840000, result.get_bytes());

You can also use the from_bytes and from_unit associated functions to create a Byte instance.

extern crate byte_unit;

use byte_unit::Byte;

let result = Byte::from_bytes(1500000);

assert_eq!(1500000, result.get_bytes());
extern crate byte_unit;

use byte_unit::{Byte, ByteUnit};

let result = Byte::from_unit(1500f64, ByteUnit::KB).unwrap();

assert_eq!(1500000, result.get_bytes());

AdjustedByte

To change the unit of a Byte instance, you can use the get_adjusted_unit method.

extern crate byte_unit;

use byte_unit::{Byte, ByteUnit};

let byte = Byte::from_str("123KiB").unwrap();

let adjusted_byte = byte.get_adjusted_unit(ByteUnit::KB);

assert_eq!("125.95 KB", adjusted_byte.to_string());

To change the unit of a Byte instance automatically and appropriately, you can use the get_appropriate_unit method.

extern crate byte_unit;

use byte_unit::Byte;

let byte = Byte::from_bytes(1500000);

let adjusted_byte = byte.get_appropriate_unit(false);

assert_eq!("1.50 MB", adjusted_byte.to_string());
extern crate byte_unit;

use byte_unit::Byte;

let byte = Byte::from_bytes(1500000);

let adjusted_byte = byte.get_appropriate_unit(true);

assert_eq!("1.43 MiB", adjusted_byte.to_string());

The number of fractional digits created by the to_string method of a AdjustedByte instance is 2 unless the ByteUnit is B.

To change the number of fractional digits in the formatted string, you can use the format method instead.

extern crate byte_unit;

use byte_unit::Byte;

let byte = Byte::from_bytes(1500000);

let adjusted_byte = byte.get_appropriate_unit(false);

assert_eq!("1.5 MB", adjusted_byte.format(1));

No Std

Disable the default features to compile this crate without std.

[dependencies.byte-unit]
version = "*"
default-features = false
features = ["u128"]

Serde Support

Enable the serde feature to support the serde framework.

[dependencies.byte-unit]
version = "*"
features = ["serde"]

Macros

n_eb_bytes

Convert n EB to bytes.

n_eib_bytes

Convert n EiB to bytes.

n_gb_bytes

Convert n GB to bytes.

n_gib_bytes

Convert n GiB to bytes.

n_kb_bytes

Convert n KB to bytes.

n_kib_bytes

Convert n KiB to bytes.

n_mb_bytes

Convert n MB to bytes.

n_mib_bytes

Convert n MiB to bytes.

n_pb_bytes

Convert n PB to bytes.

n_pib_bytes

Convert n PiB to bytes.

n_tb_bytes

Convert n TB to bytes.

n_tib_bytes

Convert n TiB to bytes.

n_zb_bytes

Convert n ZB to bytes.

n_zib_bytes

Convert n ZiB to bytes.

Structs

AdjustedByte

Generated from the get_appropriate_unit and get_adjusted_unit methods of a Byte object.

Byte

Represent the n-bytes data. Use associated functions: from_unit, from_bytes, from_str, to create the instance.

UnitIncorrectError

Errors for ByteUnit.

Enums

ByteError

Error types for Byte and ByteUnit.

ByteUnit

The unit of bytes.

ValueIncorrectError

Error types for parsing values.

Constants

EXABYTE

EB

EXBIBYTE

EiB

GIBIBYTE

GiB

GIGABYTE

GB

KIBIBYTE

KiB

KILOBYTE

KB

MEBIBYTE

MiB

MEGABYTE

MB

PEBIBYTE

PiB

PETABYTE

PB

TEBIBYTE

TiB

TERABYTE

TB

ZEBIBYTE

ZiB

ZETTABYTE

ZB

Functions

n_eb_bytes

Convert n EB to bytes.

n_eib_bytes

Convert n EiB to bytes.

n_gb_bytes

Convert n GB to bytes.

n_gib_bytes

Convert n GiB to bytes.

n_kb_bytes

Convert n KB to bytes.

n_kib_bytes

Convert n KiB to bytes.

n_mb_bytes

Convert n MB to bytes.

n_mib_bytes

Convert n MiB to bytes.

n_pb_bytes

Convert n PB to bytes.

n_pib_bytes

Convert n PiB to bytes.

n_tb_bytes

Convert n TB to bytes.

n_tib_bytes

Convert n TiB to bytes.

n_zb_bytes

Convert n ZB to bytes.

n_zib_bytes

Convert n ZiB to bytes.