Skip to main content

Crate memsizes

Crate memsizes 

Source
Expand description

Type-safe memory size newtypes with checked conversions and arithmetic.

Each unit (Bytes, KiB, MiB, GiB, etc.) is a distinct type so the compiler prevents mixing up binary and decimal sizes or raw byte counts.

use memsizes::{GiB, MiB, MB, MemorySize, Rounding};

let mem = GiB::from(2);

// Exact conversion (binary → binary)
let mib: MiB = mem.to_exact().unwrap();
assert_eq!(mib.count(), 2048);

// Rounded conversion (binary → decimal)
let mb = mem.to_rounded::<MB>(Rounding::Ceil).unwrap();

// Checked arithmetic (both operands must be the same type)
let total = mib.checked_add(MiB::from(512)).unwrap();
assert_eq!(total.count(), 2560);

Structs§

Bytes
Canonical base type: raw bytes.
EB
Exabytes (1000⁶ bytes).
EiB
Exbibytes (1024⁶ bytes).
GB
Gigabytes (1000³ bytes).
GiB
Gibibytes (1024³ bytes).
KB
Kilobytes (1000 bytes).
KiB
Kibibytes (1024 bytes).
MB
Megabytes (1000² bytes).
MiB
Mebibytes (1024² bytes).
PB
Petabytes (1000⁵ bytes).
PiB
Pebibytes (1024⁵ bytes).
TB
Terabytes (1000⁴ bytes).
TiB
Tebibytes (1024⁴ bytes).

Enums§

MemConvError
Error for failed conversions (overflow, rounding disallowed, etc.)
Rounding
How to round when a conversion isn’t an exact integer.

Traits§

MemorySize
Core trait all memory-size newtypes implement. The semantic value is “count of this unit”, stored as an integer. Each type specifies its bytes-per-unit as a u64 constant.