memsizes 0.1.0

Type-safe memory size newtypes (Bytes, KiB, MiB, GiB, KB, MB, GB) with checked conversions and arithmetic.
Documentation
  • Coverage
  • 40.74%
    11 out of 27 items documented0 out of 20 items with examples
  • Size
  • Source code size: 16.35 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 5.49 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 34s Average build duration of successful builds.
  • all releases: 31s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • aleph-im/aleph-rs
    1 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • odesenfans

memsizes

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

Stop mixing up bytes and mebibytes, or accidentally passing a decimal gigabyte where a binary one was expected. memsizes gives each unit its own type so the compiler catches the mistakes for you.

Units

Binary (IEC) Decimal (SI) Base
KiB (1024) KB (1000) Bytes
MiB (1024^2) MB (1000^2)
GiB (1024^3) GB (1000^3)

Usage

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

let mem = GiB::from_units(2);

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

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

// Checked arithmetic
let total = mib.checked_add(MiB::from_units(512)).unwrap();
assert_eq!(total.units(), 2560);

All conversions go through Bytes internally and use checked arithmetic — overflows return errors instead of wrapping.

Features

  • Zero-cost newtypes over u64
  • Compile-time unit safety between binary and decimal sizes
  • Exact conversions (to_exact) and rounded conversions (to_rounded) with floor/ceil/nearest modes
  • Checked and saturating add/sub
  • Serde support out of the box

License

MIT