1#![allow(clippy::enum_clike_unportable_variant)]
2
3use crate::marker::MetaSized;
4use crate::num::NonZero;
5use crate::ub_checks::assert_unsafe_precondition;
6use crate::{cmp, fmt, hash, mem, num};
7
8#[unstable(feature = "ptr_alignment_type", issue = "102070")]
14#[derive(Copy, Clone, PartialEq, Eq)]
15#[repr(transparent)]
16pub struct Alignment(AlignmentEnum);
17
18const _: () = assert!(size_of::<Alignment>() == size_of::<usize>());
20const _: () = assert!(align_of::<Alignment>() == align_of::<usize>());
21
22fn _alignment_can_be_structurally_matched(a: Alignment) -> bool {
23 matches!(a, Alignment::MIN)
24}
25
26impl Alignment {
27 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
40 pub const MIN: Self = Self(AlignmentEnum::_Align1Shl0);
41
42 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
47 #[inline]
48 #[must_use]
49 pub const fn of<T>() -> Self {
50 const { Alignment::new(align_of::<T>()).unwrap() }
52 }
53
54 #[inline]
69 #[must_use]
70 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
71 pub const fn of_val<T: MetaSized>(val: &T) -> Self {
72 let align = mem::align_of_val(val);
73 unsafe { Alignment::new_unchecked(align) }
75 }
76
77 #[inline]
117 #[must_use]
118 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
119 pub const unsafe fn of_val_raw<T: MetaSized>(val: *const T) -> Self {
121 let align = unsafe { mem::align_of_val_raw(val) };
123 unsafe { Alignment::new_unchecked(align) }
125 }
126
127 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
132 #[inline]
133 pub const fn new(align: usize) -> Option<Self> {
134 if align.is_power_of_two() {
135 Some(unsafe { Self::new_unchecked(align) })
137 } else {
138 None
139 }
140 }
141
142 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
151 #[inline]
152 #[track_caller]
153 pub const unsafe fn new_unchecked(align: usize) -> Self {
154 assert_unsafe_precondition!(
155 check_language_ub,
156 "Alignment::new_unchecked requires a power of two",
157 (align: usize = align) => align.is_power_of_two()
158 );
159
160 unsafe { mem::transmute::<usize, Alignment>(align) }
163 }
164
165 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
167 #[inline]
168 pub const fn as_usize(self) -> usize {
169 self.0 as usize
170 }
171
172 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
174 #[inline]
175 pub const fn as_nonzero(self) -> NonZero<usize> {
176 unsafe { mem::transmute::<Alignment, NonZero<usize>>(self) }
183 }
184
185 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
199 #[inline]
200 pub const fn log2(self) -> u32 {
201 self.as_nonzero().trailing_zeros()
202 }
203
204 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
228 #[inline]
229 pub const fn mask(self) -> usize {
230 !(unsafe { self.as_usize().unchecked_sub(1) })
232 }
233
234 pub(crate) const fn max(a: Self, b: Self) -> Self {
236 if a.as_usize() > b.as_usize() { a } else { b }
237 }
238}
239
240#[unstable(feature = "ptr_alignment_type", issue = "102070")]
241impl fmt::Debug for Alignment {
242 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
243 write!(f, "{:?} (1 << {:?})", self.as_nonzero(), self.log2())
244 }
245}
246
247#[unstable(feature = "ptr_alignment_type", issue = "102070")]
248#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
249impl const TryFrom<NonZero<usize>> for Alignment {
250 type Error = num::TryFromIntError;
251
252 #[inline]
253 fn try_from(align: NonZero<usize>) -> Result<Alignment, Self::Error> {
254 align.get().try_into()
255 }
256}
257
258#[unstable(feature = "ptr_alignment_type", issue = "102070")]
259#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
260impl const TryFrom<usize> for Alignment {
261 type Error = num::TryFromIntError;
262
263 #[inline]
264 fn try_from(align: usize) -> Result<Alignment, Self::Error> {
265 Self::new(align).ok_or(num::TryFromIntError(()))
266 }
267}
268
269#[unstable(feature = "ptr_alignment_type", issue = "102070")]
270#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
271impl const From<Alignment> for NonZero<usize> {
272 #[inline]
273 fn from(align: Alignment) -> NonZero<usize> {
274 align.as_nonzero()
275 }
276}
277
278#[unstable(feature = "ptr_alignment_type", issue = "102070")]
279#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
280impl const From<Alignment> for usize {
281 #[inline]
282 fn from(align: Alignment) -> usize {
283 align.as_usize()
284 }
285}
286
287#[unstable(feature = "ptr_alignment_type", issue = "102070")]
288impl cmp::Ord for Alignment {
289 #[inline]
290 fn cmp(&self, other: &Self) -> cmp::Ordering {
291 self.as_nonzero().get().cmp(&other.as_nonzero().get())
292 }
293}
294
295#[unstable(feature = "ptr_alignment_type", issue = "102070")]
296impl cmp::PartialOrd for Alignment {
297 #[inline]
298 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
299 Some(self.cmp(other))
300 }
301}
302
303#[unstable(feature = "ptr_alignment_type", issue = "102070")]
304impl hash::Hash for Alignment {
305 #[inline]
306 fn hash<H: hash::Hasher>(&self, state: &mut H) {
307 self.as_nonzero().hash(state)
308 }
309}
310
311#[unstable(feature = "ptr_alignment_type", issue = "102070")]
313#[rustc_const_unstable(feature = "const_default", issue = "143894")]
314impl const Default for Alignment {
315 fn default() -> Alignment {
316 Alignment::MIN
317 }
318}
319
320#[cfg(target_pointer_width = "16")]
321#[derive(Copy, Clone, PartialEq, Eq)]
322#[repr(usize)]
323enum AlignmentEnum {
324 _Align1Shl0 = 1 << 0,
325 _Align1Shl1 = 1 << 1,
326 _Align1Shl2 = 1 << 2,
327 _Align1Shl3 = 1 << 3,
328 _Align1Shl4 = 1 << 4,
329 _Align1Shl5 = 1 << 5,
330 _Align1Shl6 = 1 << 6,
331 _Align1Shl7 = 1 << 7,
332 _Align1Shl8 = 1 << 8,
333 _Align1Shl9 = 1 << 9,
334 _Align1Shl10 = 1 << 10,
335 _Align1Shl11 = 1 << 11,
336 _Align1Shl12 = 1 << 12,
337 _Align1Shl13 = 1 << 13,
338 _Align1Shl14 = 1 << 14,
339 _Align1Shl15 = 1 << 15,
340}
341
342#[cfg(target_pointer_width = "32")]
343#[derive(Copy, Clone, PartialEq, Eq)]
344#[repr(usize)]
345enum AlignmentEnum {
346 _Align1Shl0 = 1 << 0,
347 _Align1Shl1 = 1 << 1,
348 _Align1Shl2 = 1 << 2,
349 _Align1Shl3 = 1 << 3,
350 _Align1Shl4 = 1 << 4,
351 _Align1Shl5 = 1 << 5,
352 _Align1Shl6 = 1 << 6,
353 _Align1Shl7 = 1 << 7,
354 _Align1Shl8 = 1 << 8,
355 _Align1Shl9 = 1 << 9,
356 _Align1Shl10 = 1 << 10,
357 _Align1Shl11 = 1 << 11,
358 _Align1Shl12 = 1 << 12,
359 _Align1Shl13 = 1 << 13,
360 _Align1Shl14 = 1 << 14,
361 _Align1Shl15 = 1 << 15,
362 _Align1Shl16 = 1 << 16,
363 _Align1Shl17 = 1 << 17,
364 _Align1Shl18 = 1 << 18,
365 _Align1Shl19 = 1 << 19,
366 _Align1Shl20 = 1 << 20,
367 _Align1Shl21 = 1 << 21,
368 _Align1Shl22 = 1 << 22,
369 _Align1Shl23 = 1 << 23,
370 _Align1Shl24 = 1 << 24,
371 _Align1Shl25 = 1 << 25,
372 _Align1Shl26 = 1 << 26,
373 _Align1Shl27 = 1 << 27,
374 _Align1Shl28 = 1 << 28,
375 _Align1Shl29 = 1 << 29,
376 _Align1Shl30 = 1 << 30,
377 _Align1Shl31 = 1 << 31,
378}
379
380#[cfg(target_pointer_width = "64")]
381#[derive(Copy, Clone, PartialEq, Eq)]
382#[repr(usize)]
383enum AlignmentEnum {
384 _Align1Shl0 = 1 << 0,
385 _Align1Shl1 = 1 << 1,
386 _Align1Shl2 = 1 << 2,
387 _Align1Shl3 = 1 << 3,
388 _Align1Shl4 = 1 << 4,
389 _Align1Shl5 = 1 << 5,
390 _Align1Shl6 = 1 << 6,
391 _Align1Shl7 = 1 << 7,
392 _Align1Shl8 = 1 << 8,
393 _Align1Shl9 = 1 << 9,
394 _Align1Shl10 = 1 << 10,
395 _Align1Shl11 = 1 << 11,
396 _Align1Shl12 = 1 << 12,
397 _Align1Shl13 = 1 << 13,
398 _Align1Shl14 = 1 << 14,
399 _Align1Shl15 = 1 << 15,
400 _Align1Shl16 = 1 << 16,
401 _Align1Shl17 = 1 << 17,
402 _Align1Shl18 = 1 << 18,
403 _Align1Shl19 = 1 << 19,
404 _Align1Shl20 = 1 << 20,
405 _Align1Shl21 = 1 << 21,
406 _Align1Shl22 = 1 << 22,
407 _Align1Shl23 = 1 << 23,
408 _Align1Shl24 = 1 << 24,
409 _Align1Shl25 = 1 << 25,
410 _Align1Shl26 = 1 << 26,
411 _Align1Shl27 = 1 << 27,
412 _Align1Shl28 = 1 << 28,
413 _Align1Shl29 = 1 << 29,
414 _Align1Shl30 = 1 << 30,
415 _Align1Shl31 = 1 << 31,
416 _Align1Shl32 = 1 << 32,
417 _Align1Shl33 = 1 << 33,
418 _Align1Shl34 = 1 << 34,
419 _Align1Shl35 = 1 << 35,
420 _Align1Shl36 = 1 << 36,
421 _Align1Shl37 = 1 << 37,
422 _Align1Shl38 = 1 << 38,
423 _Align1Shl39 = 1 << 39,
424 _Align1Shl40 = 1 << 40,
425 _Align1Shl41 = 1 << 41,
426 _Align1Shl42 = 1 << 42,
427 _Align1Shl43 = 1 << 43,
428 _Align1Shl44 = 1 << 44,
429 _Align1Shl45 = 1 << 45,
430 _Align1Shl46 = 1 << 46,
431 _Align1Shl47 = 1 << 47,
432 _Align1Shl48 = 1 << 48,
433 _Align1Shl49 = 1 << 49,
434 _Align1Shl50 = 1 << 50,
435 _Align1Shl51 = 1 << 51,
436 _Align1Shl52 = 1 << 52,
437 _Align1Shl53 = 1 << 53,
438 _Align1Shl54 = 1 << 54,
439 _Align1Shl55 = 1 << 55,
440 _Align1Shl56 = 1 << 56,
441 _Align1Shl57 = 1 << 57,
442 _Align1Shl58 = 1 << 58,
443 _Align1Shl59 = 1 << 59,
444 _Align1Shl60 = 1 << 60,
445 _Align1Shl61 = 1 << 61,
446 _Align1Shl62 = 1 << 62,
447 _Align1Shl63 = 1 << 63,
448}