Skip to main content

wiwi_memory_usage/impls/
marker.rs

1use crate::macros::{ impl_memory_usage_static, impl_memory_usage_size_of };
2use core::marker::*;
3
4impl_memory_usage_static! {
5	{ T: ?Sized }
6	{ T: ?Sized }
7	PhantomData<T>
8	=> size_of::<PhantomData<T>>();
9}
10
11impl_memory_usage_size_of! {
12	PhantomPinned
13}
14
15#[cfg(feature = "nightly")]
16impl_memory_usage_static! {
17	{ T: ?Sized }
18	{ T: ?Sized }
19	PhantomCovariant<T>
20	=> size_of::<PhantomCovariant<T>>();
21}
22
23#[cfg(feature = "nightly")]
24impl_memory_usage_static! {
25	{ 'h }
26	{ 'h }
27	PhantomCovariantLifetime<'h>
28	=> size_of::<PhantomCovariantLifetime<'h>>();
29}
30
31#[cfg(feature = "nightly")]
32impl_memory_usage_static! {
33	{ T: ?Sized }
34	{ T: ?Sized }
35	PhantomContravariant<T>
36	=> size_of::<PhantomContravariant<T>>();
37}
38
39#[cfg(feature = "nightly")]
40impl_memory_usage_static! {
41	{ 'h }
42	{ 'h }
43	PhantomContravariantLifetime<'h>
44	=> size_of::<PhantomContravariantLifetime<'h>>();
45}
46
47#[cfg(feature = "nightly")]
48impl_memory_usage_static! {
49	{ T: ?Sized }
50	{ T: ?Sized }
51	PhantomInvariant<T>
52	=> size_of::<PhantomInvariant<T>>();
53}
54
55#[cfg(feature = "nightly")]
56impl_memory_usage_static! {
57	{ 'h }
58	{ 'h }
59	PhantomInvariantLifetime<'h>
60	=> size_of::<PhantomInvariantLifetime<'h>>();
61}
62
63#[cfg(test)]
64mod tests {
65	use super::*;
66	use crate::{
67		MemoryUsage as _,
68		MemoryUsageConst as _,
69		MemoryUsageStatic
70	};
71
72	#[test]
73	fn phantom_data_usage() {
74		let phantom = PhantomData::<usize>;
75
76		assert_eq!(phantom.memory_usage(), 0);
77		assert_eq!(phantom.memory_usage_const(), 0);
78		assert_eq!(<PhantomData<usize>>::MEMORY_USAGE, 0);
79	}
80
81	#[cfg(feature = "nightly")]
82	macro_rules! phantom_data_variance_usage {
83		{ $($test_name:ident($ty:path))* } => {
84			$(
85				#[test]
86				fn $test_name() {
87					let phantom: $ty = variance();
88
89					assert_eq!(phantom.memory_usage(), 0);
90					assert_eq!(phantom.memory_usage_const(), 0);
91					assert_eq!(<$ty>::MEMORY_USAGE, 0);
92				}
93			)*
94		}
95	}
96
97	#[cfg(feature = "nightly")]
98	phantom_data_variance_usage! {
99		phantom_covariant_usage(PhantomCovariant<usize>)
100		phantom_covariant_lifetime_usage(PhantomCovariantLifetime<'static>)
101
102		phantom_contravariant_usage(PhantomContravariant<usize>)
103		phantom_contravariant_lifetime_usage(PhantomContravariantLifetime<'static>)
104
105		phantom_invariant_usage(PhantomInvariant<usize>)
106		phantom_invariant_lifetime_usage(PhantomInvariantLifetime<'static>)
107	}
108}