Skip to main content

wiwi_memory_usage/impls/
result.rs

1use crate::{ MemoryUsage, MemoryUsageStatic };
2use crate::macros::{ impl_memory_usage, impl_memory_usage_only_static };
3
4impl_memory_usage! {
5	{ T: MemoryUsage, E: MemoryUsage }
6	{ T: [const] MemoryUsage, E: [const] MemoryUsage }
7	Result<T, E>
8	|self| match self {
9		Ok(val) => {
10			let remaining = size_of::<Result<T, E>>() - size_of::<T>();
11			val.memory_usage() + remaining
12		}
13		Err(err) => {
14			let remaining = size_of::<Result<T, E>>() - size_of::<E>();
15			err.memory_usage() + remaining
16		}
17	};
18	compacted |self| match self {
19		Ok(val) => { val.memory_usage_compacted() }
20		Err(err) => { err.memory_usage_compacted() }
21	};
22	compact |self| match self {
23		Ok(val) => { val.compact_memory() }
24		Err(err) => { err.compact_memory() }
25	};
26	ref compacted |self| match self {
27		Ok(val) => { val.memory_usage_compacted_ref() }
28		Err(err) => { err.memory_usage_compacted_ref() }
29	};
30	ref compact |self| match self {
31		Ok(val) => { val.compact_memory_ref() }
32		Err(err) => { err.compact_memory_ref() }
33	};
34}
35
36// See Option for reasoning why we can't implement MemoryUsageStatic for every
37// result type. However! if T and E are the same type, Result will always have
38// one value of it, regardless of if it ends up being stored in the Ok or the
39// Err variant. We can implement MemoryUsageStatic for this case.
40impl_memory_usage_only_static! {
41	{ TE: MemoryUsageStatic }
42	{ TE: [const] MemoryUsageStatic }
43	Result<TE, TE>
44	=> {
45		let remainder = size_of::<Result<TE, TE>>() - size_of::<TE>();
46		TE::MEMORY_USAGE + remainder
47	};
48}