[][src]Trait deepsize::DeepSizeOf

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

    fn deep_size_of(&self) -> usize { ... }
}

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

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>

This example is not tested
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
    }
}
Loading content...

Provided methods

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
);
Loading content...

Implementations on Foreign Types

impl DeepSizeOf for ()[src]

impl DeepSizeOf for bool[src]

impl DeepSizeOf for char[src]

impl DeepSizeOf for str[src]

impl DeepSizeOf for u8[src]

impl DeepSizeOf for u16[src]

impl DeepSizeOf for u32[src]

impl DeepSizeOf for u64[src]

impl DeepSizeOf for u128[src]

impl DeepSizeOf for usize[src]

impl DeepSizeOf for i8[src]

impl DeepSizeOf for i16[src]

impl DeepSizeOf for i32[src]

impl DeepSizeOf for i64[src]

impl DeepSizeOf for i128[src]

impl DeepSizeOf for isize[src]

impl DeepSizeOf for f32[src]

impl DeepSizeOf for f64[src]

impl DeepSizeOf for AtomicBool[src]

impl DeepSizeOf for AtomicI8[src]

impl DeepSizeOf for AtomicI16[src]

impl DeepSizeOf for AtomicI32[src]

impl DeepSizeOf for AtomicI64[src]

impl DeepSizeOf for AtomicIsize[src]

impl DeepSizeOf for AtomicU8[src]

impl DeepSizeOf for AtomicU16[src]

impl DeepSizeOf for AtomicU32[src]

impl DeepSizeOf for AtomicU64[src]

impl DeepSizeOf for AtomicUsize[src]

impl DeepSizeOf for NonZeroI8[src]

impl DeepSizeOf for NonZeroI16[src]

impl DeepSizeOf for NonZeroI32[src]

impl DeepSizeOf for NonZeroI64[src]

impl DeepSizeOf for NonZeroI128[src]

impl DeepSizeOf for NonZeroIsize[src]

impl DeepSizeOf for NonZeroU8[src]

impl DeepSizeOf for NonZeroU16[src]

impl DeepSizeOf for NonZeroU32[src]

impl DeepSizeOf for NonZeroU64[src]

impl DeepSizeOf for NonZeroU128[src]

impl DeepSizeOf for NonZeroUsize[src]

impl<T: ?Sized> DeepSizeOf for PhantomData<T>[src]

impl<T> DeepSizeOf for MaybeUninit<T>[src]

impl<T: Copy> DeepSizeOf for Cell<T>[src]

impl<T> DeepSizeOf for Weak<T>[src]

impl<T> DeepSizeOf for Weak<T>[src]

impl DeepSizeOf for Path[src]

impl DeepSizeOf for OsStr[src]

impl DeepSizeOf for CStr[src]

impl DeepSizeOf for PathBuf[src]

impl DeepSizeOf for OsString[src]

impl DeepSizeOf for CString[src]

impl DeepSizeOf for String[src]

impl<T: DeepSizeOf> DeepSizeOf for Option<T>[src]

impl<R: DeepSizeOf, E: DeepSizeOf> DeepSizeOf for Result<R, E>[src]

impl<T: DeepSizeOf> DeepSizeOf for RefCell<T>[src]

impl<T: DeepSizeOf> DeepSizeOf for Mutex<T>[src]

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

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

impl<T: DeepSizeOf> DeepSizeOf for RwLock<T>[src]

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

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

impl<T: DeepSizeOf> DeepSizeOf for [T; 0][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 1][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 2][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 3][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 4][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 5][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 6][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 7][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 8][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 9][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 10][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 11][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 12][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 13][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 14][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 15][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 16][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 17][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 18][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 19][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 20][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 21][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 22][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 23][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 24][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 25][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 26][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 27][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 28][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 29][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 30][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 31][src]

impl<T: DeepSizeOf> DeepSizeOf for [T; 32][src]

impl<A> DeepSizeOf for (A,) where
    A: DeepSizeOf
[src]

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

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

impl<A, B, C, D> DeepSizeOf for (A, B, C, D) where
    A: DeepSizeOf,
    B: DeepSizeOf,
    C: DeepSizeOf,
    D: DeepSizeOf
[src]

impl<A, B, C, D, E> DeepSizeOf for (A, B, C, D, E) where
    A: DeepSizeOf,
    B: DeepSizeOf,
    C: DeepSizeOf,
    D: DeepSizeOf,
    E: DeepSizeOf
[src]

impl<A, B, C, D, E, F> DeepSizeOf for (A, B, C, D, E, F) where
    A: DeepSizeOf,
    B: DeepSizeOf,
    C: DeepSizeOf,
    D: DeepSizeOf,
    E: DeepSizeOf,
    F: DeepSizeOf
[src]

impl<A, B, C, D, E, F, G> DeepSizeOf for (A, B, C, D, E, F, G) where
    A: DeepSizeOf,
    B: DeepSizeOf,
    C: DeepSizeOf,
    D: DeepSizeOf,
    E: DeepSizeOf,
    F: DeepSizeOf,
    G: DeepSizeOf
[src]

impl<A, B, C, D, E, F, G, H> DeepSizeOf for (A, B, C, D, E, F, G, H) where
    A: DeepSizeOf,
    B: DeepSizeOf,
    C: DeepSizeOf,
    D: DeepSizeOf,
    E: DeepSizeOf,
    F: DeepSizeOf,
    G: DeepSizeOf,
    H: DeepSizeOf
[src]

impl<A, B, C, D, E, F, G, H, I> DeepSizeOf for (A, B, C, D, E, F, G, H, I) where
    A: DeepSizeOf,
    B: DeepSizeOf,
    C: DeepSizeOf,
    D: DeepSizeOf,
    E: DeepSizeOf,
    F: DeepSizeOf,
    G: DeepSizeOf,
    H: DeepSizeOf,
    I: DeepSizeOf
[src]

impl<A, B, C, D, E, F, G, H, I, J> DeepSizeOf for (A, B, C, D, E, F, G, H, I, J) where
    A: DeepSizeOf,
    B: DeepSizeOf,
    C: DeepSizeOf,
    D: DeepSizeOf,
    E: DeepSizeOf,
    F: DeepSizeOf,
    G: DeepSizeOf,
    H: DeepSizeOf,
    I: DeepSizeOf,
    J: DeepSizeOf
[src]

impl<T> DeepSizeOf for Vec<T> where
    T: DeepSizeOf
[src]

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

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);

impl<T> DeepSizeOf for VecDeque<T> where
    T: DeepSizeOf
[src]

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

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);

impl<T> DeepSizeOf for LinkedList<T> where
    T: DeepSizeOf
[src]

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

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);

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

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

impl<K: Ord + DeepSizeOf, V: DeepSizeOf> DeepSizeOf for BTreeMap<K, V>[src]

impl<K: Ord + DeepSizeOf> DeepSizeOf for BTreeSet<K>[src]

impl<T: ?Sized> DeepSizeOf for Box<T> where
    T: DeepSizeOf
[src]

impl<T: ?Sized> DeepSizeOf for Arc<T> where
    T: DeepSizeOf
[src]

impl<T: ?Sized> DeepSizeOf for Rc<T> where
    T: DeepSizeOf
[src]

impl<T: ?Sized, '_> DeepSizeOf for &'_ T where
    T: DeepSizeOf
[src]

impl<T: ?Sized, '_> DeepSizeOf for &'_ mut T where
    T: DeepSizeOf
[src]

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

Loading content...

Implementors

Loading content...