wiwi/
mem_use.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use crate::prelude::*;
use self::private::SealedStruct;

pub trait MemUse {
	/// Unsure the use case, but let's check that these are dyn compatible for now?
	#[doc(hidden)]
	#[inline]
	fn __assert_dyn_compat(&self, _: &dyn MemUse, _: SealedStruct) {}

	/// Calculates total memory usage, including inline and indirect,
	#[inline]
	fn mem_use(&self) -> usize {
		self.mem_use_inline() + self.mem_use_indirect()
	}

	/// Calculates inline memory usage (ex. heap usage of a [`Vec`], always 24
	/// bytes on 64-bit architextures)
	fn mem_use_inline(&self) -> usize;

	/// Calculates indirect memory usage (ex. heap usage of a [`Vec`])
	fn mem_use_indirect(&self) -> usize;
}

pub trait MemUseStatic
where
	Self: MemUse
{
	/// Unsure the use case, but let's check that these are dyn compatible for now?
	#[doc(hidden)]
	#[inline]
	fn __assert_dyn_compat(&self, _: &dyn MemUseStatic, _: SealedStruct) {}

	/// Calculates the static memory usage for this value, including inline
	/// and indirect usage if applicable
	fn mem_use_static(&self) -> usize;
}

pub trait MemUseConst
where
	Self: Sized + MemUseStatic
{
	/// The constant memory usage for this type
	const MEM_USE: usize;
}

macro_rules! stack_only_impl {
	{ [$($generics:tt)*] $($ty:tt)* } => {
		impl<$($generics)*> MemUse for $($ty)* {
			#[inline]
			fn mem_use(&self) -> usize {
				size_of::<$($ty)*>()
			}

			#[inline]
			fn mem_use_inline(&self) -> usize {
				size_of::<$($ty)*>()
			}

			#[inline]
			fn mem_use_indirect(&self) -> usize {
				0
			}
		}

		impl<$($generics)*> MemUseStatic for $($ty)* {
			#[inline]
			fn mem_use_static(&self) -> usize {
				size_of::<$($ty)*>()
			}
		}

		impl<$($generics)*> MemUseConst for $($ty)* {
			const MEM_USE: usize = size_of::<$($ty)*>();
		}
	};

	{ $($ty:tt)* } => { stack_only_impl! { [] $($ty)* } };
}

stack_only_impl! { () }
stack_only_impl! { bool }
stack_only_impl! { char }
stack_only_impl! { u8 }
stack_only_impl! { u16 }
stack_only_impl! { u32 }
stack_only_impl! { u64 }
stack_only_impl! { u128 }
stack_only_impl! { usize }
stack_only_impl! { i8 }
stack_only_impl! { i16 }
stack_only_impl! { i32 }
stack_only_impl! { i64 }
stack_only_impl! { i128 }
stack_only_impl! { isize }
// stack_only_impl! { f16 }
stack_only_impl! { f32 }
stack_only_impl! { f64 }
// stack_only_impl! { f128 }
stack_only_impl! { [T: ?Sized] *const T }
stack_only_impl! { [T: ?Sized] *mut T }
stack_only_impl! { NonZero<u8> }
stack_only_impl! { NonZero<u16> }
stack_only_impl! { NonZero<u32> }
stack_only_impl! { NonZero<u64> }
stack_only_impl! { NonZero<u128> }
stack_only_impl! { NonZero<usize> }
stack_only_impl! { NonZero<i8> }
stack_only_impl! { NonZero<i16> }
stack_only_impl! { NonZero<i32> }
stack_only_impl! { NonZero<i64> }
stack_only_impl! { NonZero<i128> }
stack_only_impl! { NonZero<isize> }
stack_only_impl! { Saturating<u8> }
stack_only_impl! { Saturating<u16> }
stack_only_impl! { Saturating<u32> }
stack_only_impl! { Saturating<u64> }
stack_only_impl! { Saturating<u128> }
stack_only_impl! { Saturating<usize> }
stack_only_impl! { Saturating<i8> }
stack_only_impl! { Saturating<i16> }
stack_only_impl! { Saturating<i32> }
stack_only_impl! { Saturating<i64> }
stack_only_impl! { Saturating<i128> }
stack_only_impl! { Saturating<isize> }
stack_only_impl! { Wrapping<u8> }
stack_only_impl! { Wrapping<u16> }
stack_only_impl! { Wrapping<u32> }
stack_only_impl! { Wrapping<u64> }
stack_only_impl! { Wrapping<u128> }
stack_only_impl! { Wrapping<usize> }
stack_only_impl! { Wrapping<i8> }
stack_only_impl! { Wrapping<i16> }
stack_only_impl! { Wrapping<i32> }
stack_only_impl! { Wrapping<i64> }
stack_only_impl! { Wrapping<i128> }
stack_only_impl! { Wrapping<isize> }
stack_only_impl! { AtomicBool }
stack_only_impl! { AtomicU8 }
stack_only_impl! { AtomicU16 }
stack_only_impl! { AtomicU32 }
stack_only_impl! { AtomicU64 }
// stack_only_impl! { AtomicU128 }
stack_only_impl! { AtomicUsize }
stack_only_impl! { AtomicI8 }
stack_only_impl! { AtomicI16 }
stack_only_impl! { AtomicI32 }
stack_only_impl! { AtomicI64 }
// stack_only_impl! { AtomicI128 }
stack_only_impl! { AtomicIsize }
stack_only_impl! { [T] AtomicPtr<T> }
stack_only_impl! { [T: ?Sized] PhantomData<T> }
stack_only_impl! { PhantomPinned }

impl<T> MemUse for &T
where
	T: ?Sized + MemUse
{
	#[inline]
	fn mem_use_inline(&self) -> usize {
		size_of::<Self>()
	}

	#[inline]
	fn mem_use_indirect(&self) -> usize {
		T::mem_use(self)
	}
}

impl<T> MemUseStatic for &T
where
	T: ?Sized + MemUseStatic
{
	#[inline]
	fn mem_use_static(&self) -> usize {
		size_of::<Self>() + T::mem_use_static(self)
	}
}

impl<T> MemUseConst for &T
where
	T: MemUseConst
{
	const MEM_USE: usize = size_of::<Self>() + T::MEM_USE;
}

impl<T> MemUse for &mut T
where
	T: ?Sized + MemUse
{
	#[inline]
	fn mem_use_inline(&self) -> usize {
		size_of::<Self>()
	}

	#[inline]
	fn mem_use_indirect(&self) -> usize {
		T::mem_use(self)
	}
}

impl<T> MemUseStatic for &mut T
where
	T: ?Sized + MemUseStatic
{
	#[inline]
	fn mem_use_static(&self) -> usize {
		size_of::<Self>() + T::mem_use_static(self)
	}
}

impl<T> MemUseConst for &mut T
where
	T: MemUseConst
{
	const MEM_USE: usize = size_of::<Self>() + T::MEM_USE;
}

impl<T> MemUse for [T]
where
	T: MemUse
{
	#[inline]
	fn mem_use(&self) -> usize {
		self.iter().map(T::mem_use).sum()
	}

	#[inline]
	fn mem_use_inline(&self) -> usize {
		size_of_val(self)
	}

	#[inline]
	fn mem_use_indirect(&self) -> usize {
		self.iter().map(T::mem_use_indirect).sum()
	}
}

impl<T> MemUseStatic for [T]
where
	T: MemUseStatic
{
	#[inline]
	fn mem_use_static(&self) -> usize {
		let mut i = 0;
		let mut mem_use = 0;

		while i < self.len() {
			mem_use += self[i].mem_use_static();
			i += 1;
		}

		mem_use
	}
}

impl MemUse for str {
	#[inline]
	fn mem_use(&self) -> usize {
		self.len()
	}

	#[inline]
	fn mem_use_inline(&self) -> usize {
		self.len()
	}

	#[inline]
	fn mem_use_indirect(&self) -> usize {
		0
	}
}

impl MemUseStatic for str {
	#[inline]
	fn mem_use_static(&self) -> usize {
		self.len()
	}
}

// TODO: Option, tuples, Result, Vec, String, [T; N], the ranges, cells, path, osstr, cstr, rc, arc, types in this crate
// TODO: go through std's list of types lol

mod private {
	pub struct SealedStruct {
		__private: ()
	}
}