memusage 0.2.0

Small trait utility to keep track of the memory usage of structs
Documentation
//! Implementation of `MemoryReport` for `Vec` and `VecDeque`.



use crate::MemoryReport;
use alloc::{
    collections::VecDeque,
    vec::Vec,
};



impl<T: MemoryReport> MemoryReport for Vec<T> {
    const ALLOC: bool = true;
    const CHILD: bool = true;

    fn indirect(&self) -> usize {
        self.capacity() * T::direct()
    }

    fn children(&self) -> usize {
        if !(T::ALLOC || T::CHILD) { return 0; }

        self.iter().map(|x| x.indirect() + x.children()).sum()
    }
}



impl<T: MemoryReport> MemoryReport for VecDeque<T> {
    const ALLOC: bool = true;
    const CHILD: bool = true;

    fn indirect(&self) -> usize {
        self.capacity() * T::direct()
    }

    fn children(&self) -> usize {
        if !(T::ALLOC || T::CHILD) { return 0; }

        self.iter().map(|x| x.indirect() + x.children()).sum()
    }
}




#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn direct() {
        assert!((core::mem::size_of::<usize>() * 3) == Vec::<u64>::direct());
    }

    #[test]
    fn indirect() {
        assert!((core::mem::size_of::<usize>() * 4) == Vec::<u64>::with_capacity(4).indirect());
    }

    #[test]
    fn children() {
        assert!(0 == alloc::vec![0usize, 1, 2, 3].children());
    }
}