1#![allow(clippy::enum_clike_unportable_variant)]
2
3use crate::num::NonZero;
4use crate::ub_checks::assert_unsafe_precondition;
5use crate::{cmp, fmt, hash, mem, num};
6
7#[unstable(feature = "ptr_alignment_type", issue = "102070")]
13#[derive(Copy, Clone, PartialEq, Eq)]
14#[repr(transparent)]
15pub struct Alignment(AlignmentEnum);
16
17const _: () = assert!(size_of::<Alignment>() == size_of::<usize>());
19const _: () = assert!(align_of::<Alignment>() == align_of::<usize>());
20
21fn _alignment_can_be_structurally_matched(a: Alignment) -> bool {
22 matches!(a, Alignment::MIN)
23}
24
25impl Alignment {
26 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
39 pub const MIN: Self = Self(AlignmentEnum::_Align1Shl0);
40
41 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
46 #[inline]
47 #[must_use]
48 pub const fn of<T>() -> Self {
49 const { Alignment::new(align_of::<T>()).unwrap() }
51 }
52
53 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
58 #[inline]
59 pub const fn new(align: usize) -> Option<Self> {
60 if align.is_power_of_two() {
61 Some(unsafe { Self::new_unchecked(align) })
63 } else {
64 None
65 }
66 }
67
68 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
77 #[inline]
78 #[track_caller]
79 pub const unsafe fn new_unchecked(align: usize) -> Self {
80 assert_unsafe_precondition!(
81 check_language_ub,
82 "Alignment::new_unchecked requires a power of two",
83 (align: usize = align) => align.is_power_of_two()
84 );
85
86 unsafe { mem::transmute::<usize, Alignment>(align) }
89 }
90
91 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
93 #[inline]
94 pub const fn as_usize(self) -> usize {
95 self.0 as usize
96 }
97
98 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
100 #[inline]
101 pub const fn as_nonzero(self) -> NonZero<usize> {
102 unsafe { mem::transmute::<Alignment, NonZero<usize>>(self) }
109 }
110
111 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
125 #[inline]
126 pub const fn log2(self) -> u32 {
127 self.as_nonzero().trailing_zeros()
128 }
129
130 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
154 #[inline]
155 pub const fn mask(self) -> usize {
156 !(unsafe { self.as_usize().unchecked_sub(1) })
158 }
159
160 pub(crate) const fn max(a: Self, b: Self) -> Self {
162 if a.as_usize() > b.as_usize() { a } else { b }
163 }
164}
165
166#[unstable(feature = "ptr_alignment_type", issue = "102070")]
167impl fmt::Debug for Alignment {
168 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169 write!(f, "{:?} (1 << {:?})", self.as_nonzero(), self.log2())
170 }
171}
172
173#[unstable(feature = "ptr_alignment_type", issue = "102070")]
174#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
175impl const TryFrom<NonZero<usize>> for Alignment {
176 type Error = num::TryFromIntError;
177
178 #[inline]
179 fn try_from(align: NonZero<usize>) -> Result<Alignment, Self::Error> {
180 align.get().try_into()
181 }
182}
183
184#[unstable(feature = "ptr_alignment_type", issue = "102070")]
185#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
186impl const TryFrom<usize> for Alignment {
187 type Error = num::TryFromIntError;
188
189 #[inline]
190 fn try_from(align: usize) -> Result<Alignment, Self::Error> {
191 Self::new(align).ok_or(num::TryFromIntError(()))
192 }
193}
194
195#[unstable(feature = "ptr_alignment_type", issue = "102070")]
196#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
197impl const From<Alignment> for NonZero<usize> {
198 #[inline]
199 fn from(align: Alignment) -> NonZero<usize> {
200 align.as_nonzero()
201 }
202}
203
204#[unstable(feature = "ptr_alignment_type", issue = "102070")]
205#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
206impl const From<Alignment> for usize {
207 #[inline]
208 fn from(align: Alignment) -> usize {
209 align.as_usize()
210 }
211}
212
213#[unstable(feature = "ptr_alignment_type", issue = "102070")]
214impl cmp::Ord for Alignment {
215 #[inline]
216 fn cmp(&self, other: &Self) -> cmp::Ordering {
217 self.as_nonzero().get().cmp(&other.as_nonzero().get())
218 }
219}
220
221#[unstable(feature = "ptr_alignment_type", issue = "102070")]
222impl cmp::PartialOrd for Alignment {
223 #[inline]
224 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
225 Some(self.cmp(other))
226 }
227}
228
229#[unstable(feature = "ptr_alignment_type", issue = "102070")]
230impl hash::Hash for