Trait DeepSizeOf

Source
pub trait DeepSizeOf {
    // Required method
    fn deep_size_of_children(&self, context: &mut Context) -> usize;

    // Provided method
    fn deep_size_of(&self) -> usize { ... }
}
Expand description

A trait for measuring the size of an object and its children

In many cases this is just std::mem::size_of::<T>(), but if the struct contains a Vec, String, Box, or other allocated object or reference, then it is the size of the struct, plus the size of the contents of the object.

Required Methods§

Source

fn deep_size_of_children(&self, context: &mut Context) -> usize

Returns an estimation of the heap-managed storage of this object. This does not include the size of the object itself.

This is an estimation and not a precise result, because it doesn’t account for allocator’s overhead.

This is an internal function (this shouldn’t be called directly), and requires a Context to track visited references. Implementations of this function should only call deep_size_of_children, and not deep_size_of so that they reference tracking is not reset.

In all other cases, deep_size_of should be called instead of this function.

If a struct and all of its children do not allocate or have references, this method should return 0, as it cannot have any heap allocated children. There is a shortcut macro for this implementation, known_size_of, used like known_deep_size!(0, (), u32, u64); which generates the impls.

The most common way to use this method, and how the derive works, is to call this method on each of the structs members and sum the results, which works as long as all members of the struct implement DeepSizeOf.

To implement this for a collection type, you should sum the deep sizes of the items of the collection and then add the size of the allocation of the collection itself. This can become much more complicated if the collection has multiple separate allocations.

Here is an example from the implementation of DeepSizeOf for Vec<T>

impl<T> DeepSizeOf for std::vec::Vec<T> where T: DeepSizeOf {
    fn deep_size_of_children(&self, context: &mut Context) -> usize {
        // Size of heap allocations for each child
        self.iter().map(|child| child.deep_size_of_children(context)).sum()
         + self.capacity() * std::mem::size_of::<T>()  // Size of Vec's heap allocation
    }
}

Provided Methods§

Source

fn deep_size_of(&self) -> usize

Returns an estimation of a total size of memory owned by the object, including heap-managed storage.

This is an estimation and not a precise result because it doesn’t account for allocator’s overhead.

use deepsize::DeepSizeOf;

let mut map: Vec<(Box<u32>, String)> = Vec::new();

map.push((Box::new(42u32), String::from("Hello World")));
map.push((Box::new(20u32), String::from("Something")));
map.push((Box::new(0u32),  String::from("A string")));
map.push((Box::new(255u32), String::from("Dynamically Allocated!")));

assert_eq!(map.deep_size_of(),
    std::mem::size_of::<Vec<(Box<u32>, String)>>() +
    4 * std::mem::size_of::<(Box<u32>, String)>() +
    4 * std::mem::size_of::<u32>() +
    11 + 9 + 8 + 22
);

Implementations on Foreign Types§

Source§

impl DeepSizeOf for bool

Source§

impl DeepSizeOf for char

Source§

impl DeepSizeOf for f32

Source§

impl DeepSizeOf for f64

Source§

impl DeepSizeOf for i8

Source§

impl DeepSizeOf for i16

Source§

impl DeepSizeOf for i32

Source§

impl DeepSizeOf for i64

Source§

impl DeepSizeOf for i128

Source§

impl DeepSizeOf for isize

Source§

impl DeepSizeOf for str

Source§

impl DeepSizeOf for u8

Source§

impl DeepSizeOf for u16

Source§

impl DeepSizeOf for u32

Source§

impl DeepSizeOf for u64

Source§

impl DeepSizeOf for u128

Source§

impl DeepSizeOf for ()

Source§

impl DeepSizeOf for usize

Source§

impl DeepSizeOf for CString

Source§

impl DeepSizeOf for String

Source§

impl DeepSizeOf for CStr

Source§

impl DeepSizeOf for NonZero<i8>

Source§

impl DeepSizeOf for NonZero<i16>

Source§

impl DeepSizeOf for NonZero<i32>

Source§

impl DeepSizeOf for NonZero<i64>

Source§

impl DeepSizeOf for NonZero<i128>

Source§

impl DeepSizeOf for NonZero<isize>

Source§

impl DeepSizeOf for NonZero<u8>

Source§

impl DeepSizeOf for NonZero<u16>

Source§

impl DeepSizeOf for NonZero<u32>

Source§

impl DeepSizeOf for NonZero<u64>

Source§

impl DeepSizeOf for NonZero<u128>

Source§

impl DeepSizeOf for NonZero<usize>

Source§

impl DeepSizeOf for AtomicBool

Source§

impl DeepSizeOf for AtomicI8

Source§

impl DeepSizeOf for AtomicI16

Source§

impl DeepSizeOf for AtomicI32

Source§

impl DeepSizeOf for AtomicI64

Source§

impl DeepSizeOf for AtomicIsize

Source§

impl DeepSizeOf for AtomicU8

Source§

impl DeepSizeOf for AtomicU16

Source§

impl DeepSizeOf for AtomicU32

Source§

impl DeepSizeOf for AtomicU64

Source§

impl DeepSizeOf for AtomicUsize

Source§

impl DeepSizeOf for OsStr

Source§

impl DeepSizeOf for OsString

Source§

impl DeepSizeOf for Path

Source§

impl DeepSizeOf for PathBuf

Source§

impl<A> DeepSizeOf for (A,)
where A: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<A, B> DeepSizeOf for (A, B)
where A: DeepSizeOf, B: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<A, B, C> DeepSizeOf for (A, B, C)
where A: DeepSizeOf, B: DeepSizeOf, C: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<A, B, C, D> DeepSizeOf for (A, B, C, D)

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<A, B, C, D, E> DeepSizeOf for (A, B, C, D, E)

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<A, B, C, D, E, F> DeepSizeOf for (A, B, C, D, E, F)

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<A, B, C, D, E, F, G> DeepSizeOf for (A, B, C, D, E, F, G)

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<A, B, C, D, E, F, G, H> DeepSizeOf for (A, B, C, D, E, F, G, H)

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<A, B, C, D, E, F, G, H, I> DeepSizeOf for (A, B, C, D, E, F, G, H, I)

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<A, B, C, D, E, F, G, H, I, J> DeepSizeOf for (A, B, C, D, E, F, G, H, I, J)

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<K> DeepSizeOf for BTreeSet<K>
where K: Ord + DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<K, S> DeepSizeOf for HashSet<K, S>
where K: DeepSizeOf + Eq + Hash, S: BuildHasher,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<K, V> DeepSizeOf for BTreeMap<K, V>
where K: Ord + DeepSizeOf, V: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<K, V, S> DeepSizeOf for HashMap<K, V, S>
where K: DeepSizeOf + Eq + Hash, V: DeepSizeOf, S: BuildHasher,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<R, E> DeepSizeOf for Result<R, E>
where R: DeepSizeOf, E: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for Option<T>
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 0]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 1]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 2]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 3]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 4]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 5]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 6]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 7]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 8]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 9]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 10]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 11]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 12]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 13]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 14]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 15]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 16]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 17]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 18]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 19]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 20]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 21]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 22]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 23]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 24]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 25]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 26]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 27]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 28]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 29]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 30]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 31]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T; 32]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for &T
where T: DeepSizeOf + ?Sized,

Source§

fn deep_size_of_children(&self, _context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for &mut T
where T: DeepSizeOf + ?Sized,

Source§

fn deep_size_of_children(&self, _context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for [T]
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for Box<T>
where T: DeepSizeOf + ?Sized,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for LinkedList<T>
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Sums the size of each child object, assuming the overhead of each node is 2 usize (next, prev)

use deepsize::DeepSizeOf;
use std::collections::LinkedList;

let mut list: LinkedList<u8> = LinkedList::new();
for i in 0..12 {
    list.push_back(i);
}
list.push_front(13);

assert_eq!(list.deep_size_of(), std::mem::size_of::<LinkedList<u8>>()
                               + 13 * 1 + 13 * 2 * 8);
Source§

impl<T> DeepSizeOf for VecDeque<T>
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Sums the size of each child object, and then adds the size of the unused capacity.

use deepsize::DeepSizeOf;
use std::collections::VecDeque;

let mut vec: VecDeque<u8> = VecDeque::new();
for i in 0..12 {
    vec.push_back(i);
}
vec.push_front(13);

// The capacity (15?) plus four usizes (start, end, cap, pointer)
assert_eq!(vec.deep_size_of(), vec.capacity() * 1 + 32);

With allocated objects:

use deepsize::DeepSizeOf;
use std::collections::VecDeque;

let mut vec: VecDeque<Box<u64>> = VecDeque::new();
for i in 0..12 {
    vec.push_back(Box::new(i));
}
vec.push_front(Box::new(13));

// The capacity (15?) * size (8) plus four usizes (start, end, cap, pointer)
// and length (13) * the allocated size of each object
assert_eq!(vec.deep_size_of(), 32 + vec.capacity() * 8 + 13 * 8);
Source§

impl<T> DeepSizeOf for Rc<T>
where T: DeepSizeOf + ?Sized,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for Weak<T>

Source§

impl<T> DeepSizeOf for Arc<T>
where T: DeepSizeOf + ?Sized,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for Weak<T>

Source§

impl<T> DeepSizeOf for Vec<T>
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Sums the size of each child object, and then adds the size of the unused capacity.

use deepsize::DeepSizeOf;

let mut vec: Vec<u8> = vec![];
for i in 0..13 {
    vec.push(i);
}

// The capacity (16) plus three usizes (len, cap, pointer)
assert_eq!(vec.deep_size_of(), 16 + 24);

With allocated objects:

use deepsize::DeepSizeOf;

let mut vec: Vec<Box<u64>> = vec![];
for i in 0..13 {
    vec.push(Box::new(i));
}

// The capacity (16?) * size (8) plus three usizes (len, cap, pointer)
// and length (13) * the allocated size of each object
assert_eq!(vec.deep_size_of(), 24 + vec.capacity() * 8 + 13 * 8);
Source§

impl<T> DeepSizeOf for Cell<T>
where T: Copy,

Source§

impl<T> DeepSizeOf for RefCell<T>
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Source§

impl<T> DeepSizeOf for PhantomData<T>
where T: ?Sized,

Source§

impl<T> DeepSizeOf for Mutex<T>
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

This locks the Mutex, so it may deadlock; If the mutex is poisoned, this returns 0

Source§

impl<T> DeepSizeOf for RwLock<T>
where T: DeepSizeOf,

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

This reads the RwLock, so it may deadlock; If the lock is poisoned, this returns 0

Source§

impl<T> DeepSizeOf for MaybeUninit<T>

Implementors§