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