Skip to main content

wiwi_memory_usage/impls/
result.rs

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