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)]
15#[derive_const(Clone, PartialEq, Eq)]
16#[repr(transparent)]
17pub struct Alignment {
18 _inner_repr_trick: AlignmentEnum,
22}
23
24const _: () = assert!(size_of::<Alignment>() == size_of::<usize>());
26const _: () = assert!(align_of::<Alignment>() == align_of::<usize>());
27
28fn _alignment_can_be_structurally_matched(a: Alignment) -> bool {
29 matches!(a, Alignment::MIN)
30}
31
32impl Alignment {
33 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
46 pub const MIN: Self = Self::new(1).unwrap();
47
48 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
53 #[inline]
54 #[must_use]
55 pub const fn of<T>() -> Self {
56 <T as mem::SizedTypeProperties>::ALIGNMENT
57 }
58
59 #[inline]
99 #[must_use]
100 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
101 pub const fn of_val<T: MetaSized>(val: &T) -> Self {
102 let align = mem::align_of_val(val);
103 unsafe { Alignment::new_unchecked(align) }
105 }
106
107 #[inline]
155 #[must_use]
156 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
157 pub const unsafe fn of_val_raw<T: MetaSized>(val: *const T) -> Self {
158 let align = unsafe { mem::align_of_val_raw(val) };
160 unsafe { Alignment::new_unchecked(align) }
162 }
163
164 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
169 #[inline]
170 pub const fn new(align: usize) -> Option<Self> {
171 if align.is_power_of_two() {
172 Some(unsafe { Self::new_unchecked(align) })
174 } else {
175 None
176 }
177 }
178
179 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
188 #[inline]
189 #[track_caller]
190 pub const unsafe fn new_unchecked(align: usize) -> Self {
191 assert_unsafe_precondition!(
192 check_language_ub,
193 "Alignment::new_unchecked requires a power of two",
194 (align: usize = align) => align.is_power_of_two()
195 );
196
197 unsafe { mem::transmute::<usize, Alignment>(align) }
200 }
201
202 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
204 #[inline]
205 pub const fn as_usize(self) -> usize {
206 self.as_nonzero_usize().get()
210 }
211
212 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
214 #[deprecated(
215 since = "1.96.0",
216 note = "renamed to `as_nonzero_usize`",
217 suggestion = "as_nonzero_usize"
218 )]
219 #[inline]
220 pub const fn as_nonzero(self) -> NonZero<usize> {
221 self.as_nonzero_usize()
222 }
223
224 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
226 #[inline]
227 pub const fn as_nonzero_usize(self) -> NonZero<usize> {
228 unsafe { mem::transmute::<Alignment, NonZero<usize>>(self) }
235 }
236
237 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
251 #[inline]
252 pub const fn log2(self) -> u32 {
253 self.as_nonzero_usize().trailing_zeros()
254 }
255
256 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
281 #[inline]
282 pub const fn mask(self) -> usize {
283 !(unsafe { self.as_usize().unchecked_sub(1) })
285 }
286
287 pub(crate) const fn max(a: Self, b: Self) -> Self {
289 if a.as_usize() > b.as_usize() { a } else { b }
290 }
291}
292
293#[unstable(feature = "ptr_alignment_type", issue = "102070")]
294impl fmt::Debug for Alignment {
295 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
296 write!(f, "{:?} (1 << {:?})", self.as_nonzero_usize(), self.log2())
297 }
298}
299
300#[unstable(feature = "ptr_alignment_type", issue = "102070")]
301#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
302const impl TryFrom<NonZero<usize>> for Alignment {
303 type Error = num::TryFromIntError;
304
305 #[inline]
306 fn try_from(align: NonZero<usize>) -> Result<Alignment, Self::Error> {
307 align.get().try_into()
308 }
309}
310
311#[unstable(feature = "ptr_alignment_type", issue = "102070")]
312#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
313const impl TryFrom<usize> for Alignment {
314 type Error = num::TryFromIntError;
315
316 #[inline]
317 fn try_from(align: usize) -> Result<Alignment, Self::Error> {
318 Self::new(align).ok_or(num::TryFromIntError(num::IntErrorKind::NotAPowerOfTwo))
319 }
320}
321
322#[unstable(feature = "ptr_alignment_type", issue = "102070")]
323#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
324const impl From<Alignment> for NonZero<usize> {
325 #[inline]
326 fn from(align: Alignment) -> NonZero<usize> {
327 align.as_nonzero_usize()
328 }
329}
330
331#[unstable(feature = "ptr_alignment_type", issue = "102070")]
332#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
333const impl From<Alignment> for usize {
334 #[inline]
335 fn from(align: Alignment) -> usize {
336 align.as_usize()
337 }
338}
339
340#[unstable(feature = "ptr_alignment_type", issue = "102070")]
341#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
342const impl cmp::Ord for Alignment {
343 #[inline]
344 fn cmp(&self, other: &Self) -> cmp::Ordering {
345 self.as_nonzero_usize().cmp(&other.as_nonzero_usize())
346 }
347}
348
349#[unstable(feature = "ptr_alignment_type", issue = "102070")]
350#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
351const impl cmp::PartialOrd for Alignment {
352 #[inline]
353 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
354 Some(self.cmp(other))
355 }
356}
357
358#[unstable(feature = "ptr_alignment_type", issue = "102070")]
359impl hash::Hash for Alignment {
360 #[inline]
361 fn hash<H: hash::Hasher>(&self, state: &mut H) {
362 self.as_nonzero_usize().hash(state)
363 }
364}
365
366#[unstable(feature = "ptr_alignment_type", issue = "102070")]
368#[rustc_const_unstable(feature = "const_default", issue = "143894")]
369const impl Default for Alignment {
370 fn default() -> Alignment {
371 Alignment::MIN
372 }
373}
374
375#[cfg(target_pointer_width = "16")]
376#[derive(Copy)]
377#[derive_const(Clone, PartialEq, Eq)]
378#[repr(usize)]
379enum AlignmentEnum {
380 _Align1Shl0 = 1 << 0,
381 _Align1Shl1 = 1 << 1,
382 _Align1Shl2 = 1 << 2,
383 _Align1Shl3 = 1 << 3,
384 _Align1Shl4 = 1 << 4,
385 _Align1Shl5 = 1 << 5,
386 _Align1Shl6 = 1 << 6,
387 _Align1Shl7 = 1 << 7,
388 _Align1Shl8 = 1 << 8,
389 _Align1Shl9 = 1 << 9,
390 _Align1Shl10 = 1 << 10,
391 _Align1Shl11 = 1 << 11,
392 _Align1Shl12 = 1 << 12,
393 _Align1Shl13 = 1 << 13,
394 _Align1Shl14 = 1 << 14,
395 _Align1Shl15 = 1 << 15,
396}
397
398#[cfg(target_pointer_width = "32")]
399#[derive(Copy)]
400#[derive_const(Clone, PartialEq, Eq)]
401#[repr(usize)]
402enum AlignmentEnum {
403 _Align1Shl0 = 1 << 0,
404 _Align1Shl1 = 1 << 1,
405 _Align1Shl2 = 1 << 2,
406 _Align1Shl3 = 1 << 3,
407 _Align1Shl4 = 1 << 4,
408 _Align1Shl5 = 1 << 5,
409 _Align1Shl6 = 1 << 6,
410 _Align1Shl7 = 1 << 7,
411 _Align1Shl8 = 1 << 8,
412 _Align1Shl9 = 1 << 9,
413 _Align1Shl10 = 1 << 10,
414 _Align1Shl11 = 1 << 11,
415 _Align1Shl12 = 1 << 12,
416 _Align1Shl13 = 1 << 13,
417 _Align1Shl14 = 1 << 14,
418 _Align1Shl15 = 1 << 15,
419 _Align1Shl16 = 1 << 16,
420 _Align1Shl17 = 1 << 17,
421 _Align1Shl18 = 1 << 18,
422 _Align1Shl19 = 1 << 19,
423 _Align1Shl20 = 1 << 20,
424 _Align1Shl21 = 1 << 21,
425 _Align1Shl22 = 1 << 22,
426 _Align1Shl23 = 1 << 23,
427 _Align1Shl24 = 1 << 24,
428 _Align1Shl25 = 1 << 25,
429 _Align1Shl26 = 1 << 26,
430 _Align1Shl27 = 1 << 27,
431 _Align1Shl28 = 1 << 28,
432 _Align1Shl29 = 1 << 29,
433 _Align1Shl30 = 1 << 30,
434 _Align1Shl31 = 1 << 31,
435}
436
437#[cfg(target_pointer_width = "64")]
438#[derive(Copy)]
439#[derive_const(Clone, PartialEq, Eq)]
440#[repr(usize)]
441enum AlignmentEnum {
442 _Align1Shl0 = 1 << 0,
443 _Align1Shl1 = 1 << 1,
444 _Align1Shl2 = 1 << 2,
445 _Align1Shl3 = 1 << 3,
446 _Align1Shl4 = 1 << 4,
447 _Align1Shl5 = 1 << 5,
448 _Align1Shl6 = 1 << 6,
449 _Align1Shl7 = 1 << 7,
450 _Align1Shl8 = 1 << 8,
451 _Align1Shl9 = 1 << 9,
452 _Align1Shl10 = 1 << 10,
453 _Align1Shl11 = 1 << 11,
454 _Align1Shl12 = 1 << 12,
455 _Align1Shl13 = 1 << 13,
456 _Align1Shl14 = 1 << 14,
457 _Align1Shl15 = 1 << 15,
458 _Align1Shl16 = 1 << 16,
459 _Align1Shl17 = 1 << 17,
460 _Align1Shl18 = 1 << 18,
461 _Align1Shl19 = 1 << 19,
462 _Align1Shl20 = 1 << 20,
463 _Align1Shl21 = 1 << 21,
464 _Align1Shl22 = 1 << 22,
465 _Align1Shl23 = 1 << 23,
466 _Align1Shl24 = 1 << 24,
467 _Align1Shl25 = 1 << 25,
468 _Align1Shl26 = 1 << 26,
469 _Align1Shl27 = 1 << 27,
470 _Align1Shl28 = 1 << 28,
471 _Align1Shl29 = 1 << 29,
472 _Align1Shl30 = 1 << 30,
473 _Align1Shl31 = 1 << 31,
474 _Align1Shl32 = 1 << 32,
475 _Align1Shl33 = 1 << 33,
476 _Align1Shl34 = 1 << 34,
477 _Align1Shl35 = 1 << 35,
478 _Align1Shl36 = 1 << 36,
479 _Align1Shl37 = 1 << 37,
480 _Align1Shl38 = 1 << 38,
481 _Align1Shl39 = 1 << 39,
482 _Align1Shl40 = 1 << 40,
483 _Align1Shl41 = 1 << 41,
484 _Align1Shl42 = 1 << 42,
485 _Align1Shl43 = 1 << 43,
486 _Align1Shl44 = 1 << 44,
487 _Align1Shl45 = 1 << 45,
488 _Align1Shl46 = 1 << 46,
489 _Align1Shl47 = 1 << 47,
490 _Align1Shl48 = 1 << 48,
491 _Align1Shl49 = 1 << 49,
492 _Align1Shl50 = 1 << 50,
493 _Align1Shl51 = 1 << 51,
494 _Align1Shl52 = 1 << 52,
495 _Align1Shl53 = 1 << 53,
496 _Align1Shl54 = 1 << 54,
497 _Align1Shl55 = 1 << 55,
498 _Align1Shl56 = 1 << 56,
499 _Align1Shl57 = 1 << 57,
500 _Align1Shl58 = 1 << 58,
501 _Align1Shl59 = 1 << 59,
502 _Align1Shl60 = 1 << 60,
503 _Align1Shl61 = 1 << 61,
504 _Align1Shl62 = 1 << 62,
505 _Align1Shl63 = 1 << 63,
506}