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
use std::mem::size_of;

pub trait TotalSize {
    fn total_size(&self) -> usize;
}

impl<T> TotalSize for T {
    default fn total_size(&self) -> usize {
        size_of::<T>()
    }
}

#[cfg(test)]
mod tests {
    use crate::TotalSize;
    use std::mem::size_of;
    use std::ops::Deref;

    #[derive(Default)]
    struct WithHeapData {
        _int: i32,
        data: Box<u8>,
    }

    impl TotalSize for WithHeapData {
        fn total_size(&self) -> usize {
            size_of::<Self>() + self.data.deref().total_size()
        }
    }

    #[test]
    fn total_size() {
        assert_eq!(size_of::<i32>(), 5.total_size());
        assert_eq!(size_of::<String>(), String::new().total_size());

        let data = WithHeapData::default();

        assert_eq!(
            data.total_size(),
            size_of::<WithHeapData>() + size_of::<u8>()
        );
    }
}