#byte-size #human-readable #utility #proc-macro #no-std

no-std humanbyte

A procedural macro for deriving human readable byte functions

2 unstable releases

new 0.2.0 Feb 13, 2025
0.1.0 Feb 5, 2025

#334 in Value formatting

Download history 138/week @ 2025-02-04 137/week @ 2025-02-11

275 downloads per month
Used in bytescale

Apache-2.0

10KB
165 lines

Continuous Integration Crates.io Version

HumanByte

HumanByte is a procedural macro crate for deriving HumanByte functions for new types of the form NewType(u64). It is inspired by the bytesize crate (which is replicated here by our example bytescale crate).

It provides human-friendly way of constructing and displaying the type with byte units.

Usage

Add this to your Cargo.toml:

with serde support

[dependencies]
humanbyte = { version = "0.1", features = ["serde"] }

without serde support

[dependencies]
humanbyte = { version = "0.1" }

no_std compatible

[dependencies]
humanbyte = { version = "0.1", no-default-features = true }

Define your new type and derive HumanByte for it. This will derive all the necessary functions for your new type. You can then use it like this:

use humanbyte_derive::HumanByte;

#[derive(HumanByte)]
pub struct NewType(u64);

fn main() {
    let size = NewType::kib(1);
    assert_eq!(size.to_string(), "1.0 KiB");
}

See the documentation for bytescale to see more examples of what you can do with the HumanByte new type.

In addition, if you only want some of the HumanByte functions derived, you can use any of the following derives in an a la carte fashion:

  • HumanByteConstructor
  • HumanByteDisplay
  • HumanByteParse
  • HumanByteOps
  • HumanByteFromStr
  • HumanByteSerde (requires the serde feature)

Dependencies

~165KB