#primitive #thin #integer #facilitate #encapsulate #byteunit #byte-unit

simplebyteunit

A thin encapsulate for integer primitives to facilitate a fast, simple, yet ergonomic byteunit implementation

4 releases

0.2.1 Oct 1, 2023
0.2.0 Sep 29, 2023
0.1.1 Sep 27, 2023
0.1.0 Sep 27, 2023

#2049 in Rust patterns

Download history 3/week @ 2024-07-11 7/week @ 2024-07-18 23/week @ 2024-07-25 7/week @ 2024-09-05 17/week @ 2024-09-12 11/week @ 2024-09-19 19/week @ 2024-09-26 25/week @ 2024-10-03 12/week @ 2024-10-10

68 downloads per month
Used in 3 crates (2 directly)

Apache-2.0

21KB
408 lines

Rust

SimpleByteUnit

SimpleByteUnit is a crate which provides a thin encapsulate for integer primitives to facilitate a fast, simple, yet ergonomic byteunit implementation.

Usage

Add 'simplebyteunit' to your 'Cargo.toml':

[dependencies]
simplebyteunit = "0.2.0"

Example

Generate a human-readable, formatted ByteUnit:

use simplebyteunit::simplebyteunit::*;

let byteunit_var = 500000.to_byteunit(SI);

println!("{byteunit_var}");

Output:

500 kB

lib.rs:

A thin encapsulate for integer primitives to facilitate a fast, simple, yet ergonomic byteunit implementation.

Getting Started

Add 'simplebyteunit' to your 'Cargo.toml':

[dependencies]
simplebyteunit = "0.2.0"

Example

Generate a human-readable, formatted ByteUnit:

use simplebyteunit::simplebyteunit::*;

let byteunit_var = 500000.to_byteunit(SI);

assert_eq!(byteunit_var.to_string(), "500.00 kB");

Simple arithmetic operations

Addition, subtraction, multiplication, subtraction, and division are supported on this type.

use simplebyteunit::simplebyteunit::*;

let a: ByteUnit<i64> = ByteUnit::SI(500000);
let b: ByteUnit<i64> = ByteUnit::SI(500000);

assert_eq!(a + b, "1.0 MB".into());

Equal/and/or operations

Equal operations are supported on this type:

use simplebyteunit::simplebyteunit::*;

let a: ByteUnit<i64> = "500 KiB".into();
let b: ByteUnit<i64> = "500 KiB".into();

assert_eq!(a == b, true);

Or operations are also supported on this type:

use simplebyteunit::simplebyteunit::*;

let a: ByteUnit<i64> = 5000000.to_byteunit(IEC);
let b: ByteUnit<i64> = 5000000.to_byteunit(IEC);

assert_eq!(a >= b, true);

No runtime deps