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