1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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.
```rust
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);
```
All conversions go through `Bytes` internally and use checked arithmetic — overflows return errors instead of wrapping.
- ----
MIT