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]
75 #[must_use]
76 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
77 pub const fn of_val<T: MetaSized>(val: &T) -> Self {
78 let align = mem::align_of_val(val);
79 unsafe { Alignment::new_unchecked(align) }
81 }
82
83 #[inline]
122 #[must_use]
123 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
124 pub const unsafe fn of_val_raw<T: MetaSized>(val: *const T) -> Self {
125 let align = unsafe { mem::align_of_val_raw(val) };
127 unsafe { Alignment::new_unchecked(align) }
129 }
130
131 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
136 #[inline]
137 #[ferrocene::prevalidated]
138 pub const fn new(align: usize) -> Option<Self> {
139 if align.is_power_of_two() {
140 Some(unsafe { Self::new_unchecked(align) })
142 } else {
143 None
144 }
145 }
146
147 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
156 #[inline]
157 #[track_caller]
158 #[ferrocene::prevalidated]
159 pub const unsafe fn new_unchecked(align: usize) -> Self {
160 assert_unsafe_precondition!(
161 check_language_ub,
162 "Alignment::new_unchecked requires a power of two",
163 (align: usize = align) => align.is_power_of_two()
164 );
165
166 unsafe { mem::transmute::<usize, Alignment>(align) }
169 }
170
171 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
173 #[inline]
174 #[ferrocene::prevalidated]
175 pub const fn as_usize(self) -> usize {
176 self.as_nonzero_usize().get()
180 }
181
182 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
184 #[deprecated(
185 since = "CURRENT_RUSTC_VERSION",
186 note = "renamed to `as_nonzero_usize`",
187 suggestion = "as_nonzero_usize"
188 )]
189 #[inline]
190 pub const fn as_nonzero(self) -> NonZero<usize> {
191 self.as_nonzero_usize()
192 }
193
194 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
196 #[inline]
197 #[ferrocene::prevalidated]
198 pub const fn as_nonzero_usize(self) -> NonZero<usize> {
199 unsafe { mem::transmute::<Alignment, NonZero<usize>>(self) }
206 }
207
208 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
222 #[inline]
223 #[ferrocene::prevalidated]
224 pub const fn log2(self) -> u32 {
225 self.as_nonzero_usize().trailing_zeros()
226 }
227
228 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
253 #[inline]
254 pub const fn mask(self) -> usize {
255 !(unsafe { self.as_usize().unchecked_sub(1) })
257 }
258
259 pub(crate) const fn max(a: Self, b: Self) -> Self {
261 if a.as_usize() > b.as_usize() { a } else { b }
262 }
263}
264
265#[unstable(feature = "ptr_alignment_type", issue = "102070")]
266impl fmt::Debug for Alignment {
267 #[ferrocene::prevalidated]
268 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
269 write!(f, "{:?} (1 << {:?})", self.as_nonzero_usize(), self.log2())
270 }
271}
272
273#[unstable(feature = "ptr_alignment_type", issue = "102070")]
274#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
275impl const TryFrom<NonZero<usize>> for Alignment {
276 type Error = num::TryFromIntError;
277
278 #[inline]
279 fn try_from(align: NonZero<usize>) -> Result<Alignment, Self::Error> {
280 align.get().try_into()
281 }
282}
283
284#[unstable(feature = "ptr_alignment_type", issue = "102070")]
285#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
286impl const TryFrom<usize> for Alignment {
287 type Error = num::TryFromIntError;
288
289 #[inline]
290 fn try_from(align: usize) -> Result<Alignment, Self::Error> {
291 Self::new(align).ok_or(num::TryFromIntError(()))
292 }
293}
294
295#[unstable(feature = "ptr_alignment_type", issue = "102070")]
296#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
297impl const From<Alignment> for NonZero<usize> {
298 #[inline]
299 fn from(align: Alignment) -> NonZero<usize> {
300 align.as_nonzero_usize()
301 }
302}
303
304#[unstable(feature = "ptr_alignment_type", issue = "102070")]
305#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
306impl const From<Alignment> for usize {
307 #[inline]
308 fn from(align: Alignment) -> usize {
309 align.as_usize()
310 }
311}
312
313#[unstable(feature = "ptr_alignment_type", issue = "102070")]
314#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
315impl const cmp::Ord for Alignment {
316 #[inline]
317 fn cmp(&self, other: &Self) -> cmp::Ordering {
318 self.as_nonzero_usize().cmp(&other.as_nonzero_usize())
319 }
320}
321
322#[unstable(feature = "ptr_alignment_type", issue = "102070")]
323#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
324impl const cmp::PartialOrd for Alignment {
325 #[inline]
326 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
327 Some(self.cmp(other))
328 }
329}
330
331#[unstable(feature = "ptr_alignment_type", issue = "102070")]
332impl hash::Hash for Alignment {
333 #[inline]
334 #[ferrocene::prevalidated]
335 fn hash<H: hash::Hasher>(&self, state: &mut H) {
336 self.as_nonzero_usize().hash(state)
337 }
338}
339
340#[unstable(feature = "ptr_alignment_type", issue = "102070")]
342#[rustc_const_unstable(feature = "const_default", issue = "143894")]
343impl const Default for Alignment {
344 fn default() -> Alignment {
345 Alignment::MIN
346 }
347}
348
349#[cfg(target_pointer_width = "16")]
350#[derive(Copy)]
351#[derive_const(Clone, PartialEq, Eq)]
352#[repr(usize)]
353enum AlignmentEnum {
354 _Align1Shl0 = 1 << 0,
355 _Align1Shl1 = 1 << 1,
356 _Align1Shl2 = 1 << 2,
357 _Align1Shl3 = 1 << 3,
358 _Align1Shl4 = 1 << 4,
359 _Align1Shl5 = 1 << 5,
360 _Align1Shl6 = 1 << 6,
361 _Align1Shl7 = 1 << 7,
362 _Align1Shl8 = 1 << 8,
363 _Align1Shl9 = 1 << 9,
364 _Align1Shl10 = 1 << 10,
365 _Align1Shl11 = 1 << 11,
366 _Align1Shl12 = 1 << 12,
367 _Align1Shl13 = 1 << 13,
368 _Align1Shl14 = 1 << 14,
369 _Align1Shl15 = 1 << 15,
370}
371
372#[cfg(target_pointer_width = "32")]
373#[derive(Copy)]
374#[derive_const(Clone, PartialEq, Eq)]
375#[repr(usize)]
376enum AlignmentEnum {
377 _Align1Shl0 = 1 << 0,
378 _Align1Shl1 = 1 << 1,
379 _Align1Shl2 = 1 << 2,
380 _Align1Shl3 = 1 << 3,
381 _Align1Shl4 = 1 << 4,
382 _Align1Shl5 = 1 << 5,
383 _Align1Shl6 = 1 << 6,
384 _Align1Shl7 = 1 << 7,
385 _Align1Shl8 = 1 << 8,
386 _Align1Shl9 = 1 << 9,
387 _Align1Shl10 = 1 << 10,
388 _Align1Shl11 = 1 << 11,
389 _Align1Shl12 = 1 << 12,
390 _Align1Shl13 = 1 << 13,
391 _Align1Shl14 = 1 << 14,
392 _Align1Shl15 = 1 << 15,
393 _Align1Shl16 = 1 << 16,
394 _Align1Shl17 = 1 << 17,
395 _Align1Shl18 = 1 << 18,
396 _Align1Shl19 = 1 << 19,
397 _Align1Shl20 = 1 << 20,
398 _Align1Shl21 = 1 << 21,
399 _Align1Shl22 = 1 << 22,
400 _Align1Shl23 = 1 << 23,
401 _Align1Shl24 = 1 << 24,
402 _Align1Shl25 = 1 << 25,
403 _Align1Shl26 = 1 << 26,
404 _Align1Shl27 = 1 << 27,
405 _Align1Shl28 = 1 << 28,
406 _Align1Shl29 = 1 << 29,
407 _Align1Shl30 = 1 << 30,
408 _Align1Shl31 = 1 << 31,
409}
410
411#[cfg(target_pointer_width = "64")]
412#[derive(Copy)]
413#[derive_const(Clone, PartialEq, Eq)]
414#[repr(usize)]
415#[ferrocene::prevalidated]
416enum AlignmentEnum {
417 _Align1Shl0 = 1 << 0,
418 _Align1Shl1 = 1 << 1,
419 _Align1Shl2 = 1 << 2,
420 _Align1Shl3 = 1 << 3,
421 _Align1Shl4 = 1 << 4,
422 _Align1Shl5 = 1 << 5,
423 _Align1Shl6 = 1 << 6,
424 _Align1Shl7 = 1 << 7,
425 _Align1Shl8 = 1 << 8,
426 _Align1Shl9 = 1 << 9,
427 _Align1Shl10 = 1 << 10,
428 _Align1Shl11 = 1 << 11,
429 _Align1Shl12 = 1 << 12,
430 _Align1Shl13 = 1 << 13,
431 _Align1Shl14 = 1 << 14,
432 _Align1Shl15 = 1 << 15,
433 _Align1Shl16 = 1 << 16,
434 _Align1Shl17 = 1 << 17,
435 _Align1Shl18 = 1 << 18,
436 _Align1Shl19 = 1 << 19,
437 _Align1Shl20 = 1 << 20,
438 _Align1Shl21 = 1 << 21,
439 _Align1Shl22 = 1 << 22,
440 _Align1Shl23 = 1 << 23,
441 _Align1Shl24 = 1 << 24,
442 _Align1Shl25 = 1 << 25,
443 _Align1Shl26 = 1 << 26,
444 _Align1Shl27 = 1 << 27,
445 _Align1Shl28 = 1 << 28,
446 _Align1Shl29 = 1 << 29,
447 _Align1Shl30 = 1 << 30,
448 _Align1Shl31 = 1 << 31,
449 _Align1Shl32 = 1 << 32,
450 _Align1Shl33 = 1 << 33,
451 _Align1Shl34 = 1 << 34,
452 _Align1Shl35 = 1 << 35,
453 _Align1Shl36 = 1 << 36,
454 _Align1Shl37 = 1 << 37,
455 _Align1Shl38 = 1 << 38,
456 _Align1Shl39 = 1 << 39,
457 _Align1Shl40 = 1 << 40,
458 _Align1Shl41 = 1 << 41,
459 _Align1Shl42 = 1 << 42,
460 _Align1Shl43 = 1 << 43,
461 _Align1Shl44 = 1 << 44,
462 _Align1Shl45 = 1 << 45,
463 _Align1Shl46 = 1 << 46,
464 _Align1Shl47 = 1 << 47,
465 _Align1Shl48 = 1 << 48,
466 _Align1Shl49 = 1 << 49,
467 _Align1Shl50 = 1 << 50,
468 _Align1Shl51 = 1 << 51,
469 _Align1Shl52 = 1 << 52,
470 _Align1Shl53 = 1 << 53,
471 _Align1Shl54 = 1 << 54,
472 _Align1Shl55 = 1 << 55,
473 _Align1Shl56 = 1 << 56,
474 _Align1Shl57 = 1 << 57,
475 _Align1Shl58 = 1 << 58,
476 _Align1Shl59 = 1 << 59,
477 _Align1Shl60 = 1 << 60,
478 _Align1Shl61 = 1 << 61,
479 _Align1Shl62 = 1 << 62,
480 _Align1Shl63 = 1 << 63,
481}