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