size_of/support/
hashbrown.rs

1#![cfg(feature = "hashbrown")]
2
3use crate::{std_impls::hashmap::estimate_hashmap_size, Context, SizeOf};
4use hashbrown::{HashMap, HashSet};
5
6impl<K, S> SizeOf for HashSet<K, S>
7where
8    K: SizeOf,
9    S: SizeOf,
10{
11    fn size_of_children(&self, context: &mut Context) {
12        if self.capacity() != 0 {
13            let (total_bytes, used_bytes) =
14                estimate_hashmap_size::<K, ()>(self.len(), self.capacity());
15
16            context
17                .add(used_bytes)
18                .add_excess(total_bytes - used_bytes)
19                .add_distinct_allocation();
20
21            self.iter().for_each(|key| key.size_of_children(context));
22        }
23
24        self.hasher().size_of_children(context);
25    }
26}
27
28impl<K, V, S> SizeOf for HashMap<K, V, S>
29where
30    K: SizeOf,
31    V: SizeOf,
32    S: SizeOf,
33{
34    fn size_of_children(&self, context: &mut Context) {
35        if self.capacity() != 0 {
36            let (total_bytes, used_bytes) =
37                estimate_hashmap_size::<K, V>(self.len(), self.capacity());
38
39            context
40                .add(used_bytes)
41                .add_excess(total_bytes - used_bytes)
42                .add_distinct_allocation();
43
44            self.iter().for_each(|(key, value)| {
45                key.size_of_children(context);
46                value.size_of_children(context);
47            });
48        }
49
50        self.hasher().size_of_children(context);
51    }
52}