1#![allow(
2 stable_features,
3 reason = "new_range_api, new_range_from_api, new_range_to_inclusive_api is not stable in msrv"
4)]
5#![cfg_attr(
6 feature = "nightly",
7 feature(
8 const_trait_impl,
9 f16,
10 f128,
11 integer_atomics,
12 new_range_api,
13 new_range_from_api,
14 new_range_to_inclusive_api,
15 phantom_variance_markers
16 )
17)]
18#![cfg_attr(
19 not(feature = "std"),
20 no_std
21)]
22#![deny(
23 unconditional_recursion,
24 clippy::unconditional_recursion
25)]
26
27mod macros;
28
29#[diagnostic::on_unimplemented(
31 message = "Type doesn't know its memory usage"
32)]
33pub trait MemoryUsage {
34 fn memory_usage(&self) -> usize;
36
37 #[inline]
46 fn memory_usage_compacted(&self) -> usize {
47 self.memory_usage_compacted_ref()
51 }
52
53 #[inline]
56 fn compact_memory(&mut self) -> usize {
57 self.compact_memory_ref()
61 }
62
63 #[inline]
72 fn memory_usage_compacted_ref(&self) -> usize {
73 self.memory_usage()
75 }
76
77 #[inline]
87 fn compact_memory_ref(&self) -> usize {
88 0
90 }
91}
92
93macro_rules! const_trait {
96 () => {
97 #[diagnostic::on_unimplemented(
99 message = "Type doesn't know its memory usage at compile time"
100 )]
101 pub trait MemoryUsageConst: MemoryUsage {
102 fn memory_usage_const(&self) -> usize;
103 }
104
105 #[diagnostic::on_unimplemented(
108 message = "Type doesn't know its static memory usage"
109 )]
110 pub trait MemoryUsageStatic: MemoryUsageConst {
111 const MEMORY_USAGE: usize;
112 }
113 };
114
115 (const) => {
116 #[diagnostic::on_unimplemented(
118 message = "Type doesn't know its memory usage at compile time"
119 )]
120 pub const trait MemoryUsageConst: MemoryUsage {
121 fn memory_usage_const(&self) -> usize;
122 }
123
124 #[diagnostic::on_unimplemented(
127 message = "Type doesn't know its static memory usage"
128 )]
129 pub const trait MemoryUsageStatic: MemoryUsageConst {
130 const MEMORY_USAGE: usize;
131 }
132 };
133}
134
135#[cfg(not(feature = "nightly"))]
136const_trait!();
137
138#[cfg(feature = "nightly")]
139const_trait!(const);
140
141mod impls {
142 mod array;
146 mod atomic;
147 mod bool;
148 mod char;
149 mod marker;
150 mod nonzero;
151 mod number;
152 mod option;
153 mod pointer;
154 mod range;
155 mod reference;
156 mod result;
157 mod saturating;
158 mod slice;
159 mod string;
160 mod unit;
161 mod vec;
162 mod wrapping;
163}
164
165#[doc(hidden)]
166pub mod __macro_support {
167 pub(crate) use crate::macros::{
168 impl_memory_usage,
169 impl_memory_usage_only_const,
170 impl_memory_usage_const,
171 impl_memory_usage_only_static,
172 impl_memory_usage_static
173 };
174 pub use wiwi_macro_proc::{
175 memory_usage_cfg_nightly as cfg_nightly,
176 memory_usage_cfg_not_nightly as cfg_not_nightly
177 };
178}