Skip to main content

priority_lfu/deepsize/
external_impls.rs

1#[cfg(feature = "slotmap")]
2mod slotmap_impl {
3	use core::mem::size_of;
4
5	use crate::{Context, DeepSizeOf, known_deep_size};
6
7	known_deep_size!(0; slotmap::KeyData, slotmap::DefaultKey);
8
9	impl<K, V> DeepSizeOf for slotmap::SlotMap<K, V>
10	where
11		K: DeepSizeOf + slotmap::Key,
12		V: DeepSizeOf,
13	{
14		fn deep_size_of_children(&self, context: &mut Context) -> usize {
15			self.iter().fold(0, |sum, (key, val)| {
16				sum + key.deep_size_of_children(context) + val.deep_size_of_children(context)
17			}) + self.capacity() * size_of::<(u32, V)>()
18		}
19	}
20}
21
22#[cfg(feature = "slab")]
23mod slab_impl {
24	use core::mem::size_of;
25
26	use crate::{Context, DeepSizeOf};
27
28	// Mirror's `slab`'s internal `Entry` struct
29	enum MockEntry<T> {
30		_Vacant(usize),
31		_Occupied(T),
32	}
33
34	impl<T> DeepSizeOf for slab::Slab<T>
35	where
36		T: DeepSizeOf,
37	{
38		fn deep_size_of_children(&self, context: &mut Context) -> usize {
39			let capacity_size = self.capacity() * size_of::<MockEntry<T>>();
40			let owned_size =
41				self.iter().fold(0, |sum, (_, val)| sum + val.deep_size_of_children(context));
42			capacity_size + owned_size
43		}
44	}
45}
46
47#[cfg(feature = "arrayvec")]
48mod arrayvec_impl {
49	use crate::{Context, DeepSizeOf};
50
51	impl<T: DeepSizeOf, const CAP: usize> DeepSizeOf for arrayvec::ArrayVec<T, CAP> {
52		fn deep_size_of_children(&self, context: &mut Context) -> usize {
53			self.iter().fold(0, |sum, elem| sum + elem.deep_size_of_children(context))
54		}
55	}
56
57	impl<const CAP: usize> DeepSizeOf for arrayvec::ArrayString<CAP> {
58		fn deep_size_of_children(&self, _context: &mut Context) -> usize {
59			0
60		}
61	}
62}
63
64#[cfg(feature = "smallvec")]
65mod smallvec_impl {
66	use core::mem::size_of;
67
68	use crate::{Context, DeepSizeOf};
69
70	impl<A> DeepSizeOf for smallvec::SmallVec<A>
71	where
72		A: smallvec::Array,
73		<A as smallvec::Array>::Item: DeepSizeOf,
74	{
75		fn deep_size_of_children(&self, context: &mut Context) -> usize {
76			let child_size =
77				self.iter().fold(0, |sum, elem| sum + elem.deep_size_of_children(context));
78			if self.spilled() {
79				child_size + self.capacity() * size_of::<<A as smallvec::Array>::Item>()
80			} else {
81				child_size
82			}
83		}
84	}
85}
86
87mod hashbrown_impl {
88	use core::mem::size_of;
89
90	use crate::{Context, DeepSizeOf};
91
92	// This is probably still incorrect, but it's better than before
93	impl<K, V, S> DeepSizeOf for hashbrown::HashMap<K, V, S>
94	where
95		K: DeepSizeOf + Eq + std::hash::Hash,
96		V: DeepSizeOf,
97		S: std::hash::BuildHasher,
98	{
99		fn deep_size_of_children(&self, context: &mut Context) -> usize {
100			self.iter().fold(0, |sum, (key, val)| {
101				sum + key.deep_size_of_children(context) + val.deep_size_of_children(context)
102			}) + self.capacity() * size_of::<(K, V)>()
103			// Buckets would be the more correct value, but there isn't
104			// an API for accessing that with hashbrown.
105			// I believe that hashbrown's HashTable is represented as
106			// an array of (K, V), with control bytes at the start/end
107			// that mark used/uninitialized buckets (?)
108		}
109	}
110
111	impl<K, S> DeepSizeOf for hashbrown::HashSet<K, S>
112	where
113		K: DeepSizeOf + Eq + std::hash::Hash,
114		S: std::hash::BuildHasher,
115	{
116		fn deep_size_of_children(&self, context: &mut Context) -> usize {
117			self.iter().fold(0, |sum, key| sum + key.deep_size_of_children(context))
118				+ self.capacity() * size_of::<K>()
119		}
120	}
121}
122
123#[cfg(feature = "chrono")]
124mod chrono_impl {
125	use chrono::*;
126
127	use crate::known_deep_size;
128
129	known_deep_size!(0;
130		NaiveDate, NaiveTime, NaiveDateTime, IsoWeek,
131		Duration, Month, Weekday,
132		FixedOffset, Local, Utc,
133		{T: TimeZone} DateTime<T>
134	);
135}
136
137#[cfg(feature = "uuid")]
138mod uuid_impl {
139	use uuid::Uuid;
140
141	use crate::known_deep_size;
142
143	known_deep_size!(0; Uuid);
144}
145
146#[cfg(feature = "geo")]
147mod geo_impl {
148	use crate::known_deep_size;
149
150	known_deep_size!(0; geo::Point, geo::LineString, geo::Polygon, geo::MultiPoint, geo::MultiLineString, geo::MultiPolygon);
151}
152
153#[cfg(feature = "regex")]
154mod regex_impl {
155	use crate::known_deep_size;
156
157	known_deep_size!(0; regex::Regex);
158}
159
160#[cfg(feature = "rust_decimal")]
161mod rust_decimal_impl {
162	use crate::known_deep_size;
163
164	known_deep_size!(0; rust_decimal::Decimal);
165}
166
167#[cfg(feature = "bytes")]
168mod bytes_impl {
169	use crate::known_deep_size;
170
171	known_deep_size!(0; bytes::Bytes);
172}