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