dyn_size_of is the Rust library by Piotr Beling to report approximate amount of memory consumed by variables, including the memory allocated on heap.

Examples

Simple usage

```rust use dynsizeof::GetSize;

let bs = vec![1u32, 2u32, 3u32].intoboxedslice(); asserteq!(bs.sizebytesdyn(), 3*4); asserteq!(bs.sizebytes(), 3*4 + std::mem::sizeof_val(&bs)); ```

Implementing GetSize for a custom type

```rust use dynsizeof::GetSize;

struct NoHeapMem { a: u32, b: u8 }

// default implementation is fine for types that do not use heap allocations impl GetSize for NoHeapMem {}

struct WithHeapMem { a: Vec, b: Vec, c: u32 }

// For types that use heap allocations: impl GetSize for WithHeapMem { // sizebytesdyn must be implemented and return amount of heap memory used fn sizebytesdyn(&self) -> usize { self.a.sizebytesdyn() + self.b.sizebytesdyn() } // USESDYNMEM must be set to true const USESDYNMEM: bool = true; }

let s = NoHeapMem { a: 1, b: 2 }; asserteq!(NoHeapMem::USESDYNMEM, false); asserteq!(s.sizebytesdyn(), 0); asserteq!(s.sizebytes(), std::mem::sizeofval(&s));

let d = WithHeapMem { a: vec![1, 2], b: vec![3, 4], c: 5 }; asserteq!(WithHeapMem::USESDYNMEM, true); asserteq!(d.sizebytesdyn(), 24 + 21); asserteq!(d.sizebytes(), 24 + 21 + std::mem::sizeofval(&d)); ```