Skip to main content

grafeo_common/memory/
reporter.rs

1//! Memory introspection trait for reporting heap usage.
2
3/// Trait for components that can report their memory usage.
4///
5/// Implementations should estimate heap memory owned by the component,
6/// using `capacity() * element_size` for collections and `size_of::<T>()`
7/// for fixed-size fields. The goal is a useful approximation, not an
8/// exact byte count.
9pub trait MemoryReporter {
10    /// Returns estimated heap memory usage in bytes.
11    ///
12    /// This should include:
13    /// - Heap allocations owned by this component (Vec capacity, HashMap buckets, etc.)
14    /// - Nested heap allocations (String contents, Box contents)
15    ///
16    /// This should NOT include:
17    /// - The size of `self` on the stack (the caller adds that if needed)
18    /// - Memory owned by other components (avoid double-counting)
19    fn heap_memory_bytes(&self) -> usize;
20
21    /// Returns the number of logical items stored.
22    ///
23    /// For a hash map this might be `len()`, for an index the number of entries,
24    /// for a cache the number of cached items.
25    fn item_count(&self) -> usize {
26        0
27    }
28}