Trait mm0_deepsize::DeepSizeOf[][src]

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

    fn deep_size_of(&self) -> usize { ... }
fn deep_size_of_with(&self, context: &mut Context) -> 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

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.

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 implmeent 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 seperate 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

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.

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

Implementations on Foreign Types

Implementors