memusage/builtin/
slice.rs1use crate::MemoryReport;
6
7
8
9impl<T: MemoryReport> MemoryReport for &[T] {
10    const ALLOC: bool = false;
11    const CHILD: bool = true;
12
13    fn indirect(&self) -> usize {
14        self.len() * T::direct()
15    }
16
17    fn children(&self) -> usize {
18        if !(T::ALLOC || T::CHILD) { return 0; }
19
20        self.iter().map(|x| x.indirect() + x.children()).sum()
21    }
22}
23
24impl<T: MemoryReport, const N: usize> MemoryReport for &[T; N] {
25    const ALLOC: bool = false;
26    const CHILD: bool = true;
27
28    fn indirect(&self) -> usize {
29        N * T::direct()
30    }
31
32    fn children(&self) -> usize {
33        if !(T::ALLOC || T::CHILD) { return 0; }
34
35        self.iter().map(|x| x.indirect() + x.children()).sum()
36    }
37}
38
39
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn direct() {
47        assert!((core::mem::size_of::<usize>() * 2) == <&[u8]>::direct());
48        assert!((core::mem::size_of::<usize>()    ) == <&[u8; 2]>::direct());
49    }
50
51    #[test]
52    fn indirect() {
53        let array = [0u8, 1, 2, 3];
54        assert!((core::mem::size_of::<u8>() * 4) == (&array).indirect());
55        assert!((core::mem::size_of::<u8>() * 4) == (&array[..]).indirect());
56    }
57
58    #[test]
59    fn children() {
60        let array = [0u8, 1, 2, 3];
61
62        assert!(0 == (&array).children());
63        assert!(0 == (&array[..]).children());
64    }
65}