memusage/builtin/hashset.rs
1//! Implementation of `MemoryReport` for `HashSet`.
2//! The current `HashSet` implementation uses `HashMap<T, ()>` under the hood.
3//! This makes it a bit difficult to estimate the actual size used due to the
4//! possible optimizations that the compiler may have applied to the `()` type.
5//! The current implementation tries it's best to estimate it's size but does
6//! not guarantee to be exact.
7
8
9
10use crate::MemoryReport;
11use std::collections::HashSet;
12
13
14
15impl<T: MemoryReport> MemoryReport for HashSet<T> {
16 const ALLOC: bool = true;
17 const CHILD: bool = true;
18
19 fn indirect(&self) -> usize {
20 self.capacity() * T::direct()
21 }
22
23 fn children(&self) -> usize {
24 if T::ALLOC || T::CHILD {
25 self.iter().map(|x| x.indirect() + x.children()).sum()
26 } else {
27 0
28 }
29 }
30}