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