1 use crate::convert::TryFrom;
2 use crate::intrinsics::assert_unsafe_precondition;
3 use crate::num::NonZeroUsize;
4 use crate::{cmp, fmt, hash, mem, num};
6 /// A type storing a `usize` which is a power of two, and thus
7 /// represents a possible alignment in the rust abstract machine.
9 /// Note that particularly large alignments, while representable in this type,
10 /// are likely not to be supported by actual allocators and linkers.
11 #[derive(Copy, Clone)]
13 pub(crate) struct ValidAlign(ValidAlignEnum);
15 // ValidAlign is `repr(usize)`, but via extra steps.
16 const _: () = assert!(mem::size_of::<ValidAlign>() == mem::size_of::<usize>());
17 const _: () = assert!(mem::align_of::<ValidAlign>() == mem::align_of::<usize>());
20 /// Creates a `ValidAlign` from a power-of-two `usize`.
24 /// `align` must be a power of two.
26 /// Equivalently, it must be `1 << exp` for some `exp` in `0..usize::BITS`.
27 /// It must *not* be zero.
29 pub(crate) const unsafe fn new_unchecked(align: usize) -> Self {
30 // SAFETY: Precondition passed to the caller.
31 unsafe { assert_unsafe_precondition!((align: usize) => align.is_power_of_two()) };
33 // SAFETY: By precondition, this must be a power of two, and
34 // our variants encompass all possible powers of two.
35 unsafe { mem::transmute::<usize, ValidAlign>(align) }
39 pub(crate) const fn as_usize(self) -> usize {
44 pub(crate) const fn as_nonzero(self) -> NonZeroUsize {
45 // SAFETY: All the discriminants are non-zero.
46 unsafe { NonZeroUsize::new_unchecked(self.as_usize()) }
49 /// Returns the base 2 logarithm of the alignment.
51 /// This is always exact, as `self` represents a power of two.
53 pub(crate) fn log2(self) -> u32 {
54 self.as_nonzero().trailing_zeros()
57 /// Returns the alignment for a type.
59 pub(crate) fn of<T>() -> Self {
60 // SAFETY: rustc ensures that type alignment is always a power of two.
61 unsafe { ValidAlign::new_unchecked(mem::align_of::<T>()) }
65 impl fmt::Debug for ValidAlign {
66 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 write!(f, "{:?} (1 << {:?})", self.as_nonzero(), self.log2())
71 impl TryFrom<NonZeroUsize> for ValidAlign {
72 type Error = num::TryFromIntError;
75 fn try_from(align: NonZeroUsize) -> Result<ValidAlign, Self::Error> {
76 if align.is_power_of_two() {
77 // SAFETY: Just checked for power-of-two
78 unsafe { Ok(ValidAlign::new_unchecked(align.get())) }
80 Err(num::TryFromIntError(()))
85 impl TryFrom<usize> for ValidAlign {
86 type Error = num::TryFromIntError;
89 fn try_from(align: usize) -> Result<ValidAlign, Self::Error> {
90 if align.is_power_of_two() {
91 // SAFETY: Just checked for power-of-two
92 unsafe { Ok(ValidAlign::new_unchecked(align)) }
94 Err(num::TryFromIntError(()))
99 impl cmp::Eq for ValidAlign {}
101 impl cmp::PartialEq for ValidAlign {
103 fn eq(&self, other: &Self) -> bool {
104 self.as_nonzero() == other.as_nonzero()
108 impl cmp::Ord for ValidAlign {
110 fn cmp(&self, other: &Self) -> cmp::Ordering {
111 self.as_nonzero().cmp(&other.as_nonzero())
115 impl cmp::PartialOrd for ValidAlign {
117 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
118 Some(self.cmp(other))
122 impl hash::Hash for ValidAlign {
124 fn hash<H: hash::Hasher>(&self, state: &mut H) {
125 self.as_nonzero().hash(state)
129 #[cfg(target_pointer_width = "16")]
130 type ValidAlignEnum = ValidAlignEnum16;
131 #[cfg(target_pointer_width = "32")]
132 type ValidAlignEnum = ValidAlignEnum32;
133 #[cfg(target_pointer_width = "64")]
134 type ValidAlignEnum = ValidAlignEnum64;
136 #[derive(Copy, Clone)]
138 enum ValidAlignEnum16 {
139 _Align1Shl0 = 1 << 0,
140 _Align1Shl1 = 1 << 1,
141 _Align1Shl2 = 1 << 2,
142 _Align1Shl3 = 1 << 3,
143 _Align1Shl4 = 1 << 4,
144 _Align1Shl5 = 1 << 5,
145 _Align1Shl6 = 1 << 6,
146 _Align1Shl7 = 1 << 7,
147 _Align1Shl8 = 1 << 8,
148 _Align1Shl9 = 1 << 9,
149 _Align1Shl10 = 1 << 10,
150 _Align1Shl11 = 1 << 11,
151 _Align1Shl12 = 1 << 12,
152 _Align1Shl13 = 1 << 13,
153 _Align1Shl14 = 1 << 14,
154 _Align1Shl15 = 1 << 15,
157 #[derive(Copy, Clone)]
159 enum ValidAlignEnum32 {
160 _Align1Shl0 = 1 << 0,
161 _Align1Shl1 = 1 << 1,
162 _Align1Shl2 = 1 << 2,
163 _Align1Shl3 = 1 << 3,
164 _Align1Shl4 = 1 << 4,
165 _Align1Shl5 = 1 << 5,
166 _Align1Shl6 = 1 << 6,
167 _Align1Shl7 = 1 << 7,
168 _Align1Shl8 = 1 << 8,
169 _Align1Shl9 = 1 << 9,
170 _Align1Shl10 = 1 << 10,
171 _Align1Shl11 = 1 << 11,
172 _Align1Shl12 = 1 << 12,
173 _Align1Shl13 = 1 << 13,
174 _Align1Shl14 = 1 << 14,
175 _Align1Shl15 = 1 << 15,
176 _Align1Shl16 = 1 << 16,
177 _Align1Shl17 = 1 << 17,
178 _Align1Shl18 = 1 << 18,
179 _Align1Shl19 = 1 << 19,
180 _Align1Shl20 = 1 << 20,
181 _Align1Shl21 = 1 << 21,
182 _Align1Shl22 = 1 << 22,
183 _Align1Shl23 = 1 << 23,
184 _Align1Shl24 = 1 << 24,
185 _Align1Shl25 = 1 << 25,
186 _Align1Shl26 = 1 << 26,
187 _Align1Shl27 = 1 << 27,
188 _Align1Shl28 = 1 << 28,
189 _Align1Shl29 = 1 << 29,
190 _Align1Shl30 = 1 << 30,
191 _Align1Shl31 = 1 << 31,
194 #[derive(Copy, Clone)]
196 enum ValidAlignEnum64 {
197 _Align1Shl0 = 1 << 0,
198 _Align1Shl1 = 1 << 1,
199 _Align1Shl2 = 1 << 2,
200 _Align1Shl3 = 1 << 3,
201 _Align1Shl4 = 1 << 4,
202 _Align1Shl5 = 1 << 5,
203 _Align1Shl6 = 1 << 6,
204 _Align1Shl7 = 1 << 7,
205 _Align1Shl8 = 1 << 8,
206 _Align1Shl9 = 1 << 9,
207 _Align1Shl10 = 1 << 10,
208 _Align1Shl11 = 1 << 11,
209 _Align1Shl12 = 1 << 12,
210 _Align1Shl13 = 1 << 13,
211 _Align1Shl14 = 1 << 14,
212 _Align1Shl15 = 1 << 15,
213 _Align1Shl16 = 1 << 16,
214 _Align1Shl17 = 1 << 17,
215 _Align1Shl18 = 1 << 18,
216 _Align1Shl19 = 1 << 19,
217 _Align1Shl20 = 1 << 20,
218 _Align1Shl21 = 1 << 21,
219 _Align1Shl22 = 1 << 22,
220 _Align1Shl23 = 1 << 23,
221 _Align1Shl24 = 1 << 24,
222 _Align1Shl25 = 1 << 25,
223 _Align1Shl26 = 1 << 26,
224 _Align1Shl27 = 1 << 27,
225 _Align1Shl28 = 1 << 28,
226 _Align1Shl29 = 1 << 29,
227 _Align1Shl30 = 1 << 30,
228 _Align1Shl31 = 1 << 31,
229 _Align1Shl32 = 1 << 32,
230 _Align1Shl33 = 1 << 33,
231 _Align1Shl34 = 1 << 34,
232 _Align1Shl35 = 1 << 35,
233 _Align1Shl36 = 1 << 36,
234 _Align1Shl37 = 1 << 37,
235 _Align1Shl38 = 1 << 38,
236 _Align1Shl39 = 1 << 39,
237 _Align1Shl40 = 1 << 40,
238 _Align1Shl41 = 1 << 41,
239 _Align1Shl42 = 1 << 42,
240 _Align1Shl43 = 1 << 43,
241 _Align1Shl44 = 1 << 44,
242 _Align1Shl45 = 1 << 45,
243 _Align1Shl46 = 1 << 46,
244 _Align1Shl47 = 1 << 47,
245 _Align1Shl48 = 1 << 48,
246 _Align1Shl49 = 1 << 49,
247 _Align1Shl50 = 1 << 50,
248 _Align1Shl51 = 1 << 51,
249 _Align1Shl52 = 1 << 52,
250 _Align1Shl53 = 1 << 53,
251 _Align1Shl54 = 1 << 54,
252 _Align1Shl55 = 1 << 55,
253 _Align1Shl56 = 1 << 56,
254 _Align1Shl57 = 1 << 57,
255 _Align1Shl58 = 1 << 58,
256 _Align1Shl59 = 1 << 59,
257 _Align1Shl60 = 1 << 60,
258 _Align1Shl61 = 1 << 61,
259 _Align1Shl62 = 1 << 62,
260 _Align1Shl63 = 1 << 63,