1use crate::num::NonZero;
2use crate::ub_checks::assert_unsafe_precondition;
3use crate::{cmp, fmt, hash, mem, num};
4
5#[unstable(feature = "ptr_alignment_type", issue = "102070")]
11#[derive(Copy, Clone, PartialEq, Eq)]
12#[repr(transparent)]
13pub struct Alignment(AlignmentEnum);
14
15const _: () = assert!(mem::size_of::<Alignment>() == mem::size_of::<usize>());
17const _: () = assert!(mem::align_of::<Alignment>() == mem::align_of::<usize>());
18
19fn _alignment_can_be_structurally_matched(a: Alignment) -> bool {
20 matches!(a, Alignment::MIN)
21}
22
23impl Alignment {
24 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
37 pub const MIN: Self = Self(AlignmentEnum::_Align1Shl0);
38
39 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
44 #[inline]
45 #[must_use]
46 pub const fn of<T>() -> Self {
47 const { Alignment::new(mem::align_of::<T>()).unwrap() }
49 }
50
51 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
56 #[inline]
57 pub const fn new(align: usize) -> Option<Self> {
58 if align.is_power_of_two() {
59 Some(unsafe { Self::new_unchecked(align) })
61 } else {
62 None
63 }
64 }
65
66 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
75 #[inline]
76 pub const unsafe fn new_unchecked(align: usize) -> Self {
77 assert_unsafe_precondition!(
78 check_language_ub,
79 "Alignment::new_unchecked requires a power of two",
80 (align: usize = align) => align.is_power_of_two()
81 );
82
83 unsafe { mem::transmute::<usize, Alignment>(align) }
86 }
87
88 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
90 #[inline]
91 pub const fn as_usize(self) -> usize {
92 self.0 as usize
93 }
94
95 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
97 #[inline]
98 pub const fn as_nonzero(self) -> NonZero<usize> {
99 unsafe { mem::transmute::<Alignment, NonZero<usize>>(self) }
106 }
107
108 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
122 #[inline]
123 pub const fn log2(self) -> u32 {
124 self.as_nonzero().trailing_zeros()
125 }
126
127 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
151 #[inline]
152 pub const fn mask(self) -> usize {
153 !(unsafe { self.as_usize().unchecked_sub(1) })
155 }
156
157 pub(crate) const fn max(a: Self, b: Self) -> Self {
159 if a.as_usize() > b.as_usize() { a } else { b }
160 }
161}
162
163#[unstable(feature = "ptr_alignment_type", issue = "102070")]
164impl fmt::Debug for Alignment {
165 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
166 write!(f, "{:?} (1 << {:?})", self.as_nonzero(), self.log2())
167 }
168}
169
170#[unstable(feature = "ptr_alignment_type", issue = "102070")]
171impl TryFrom<NonZero<usize>> for Alignment {
172 type Error = num::TryFromIntError;
173
174 #[inline]
175 fn try_from(align: NonZero<usize>) -> Result<Alignment, Self::Error> {
176 align.get().try_into()
177 }
178}
179
180#[unstable(feature = "ptr_alignment_type", issue = "102070")]
181impl TryFrom<usize> for Alignment {
182 type Error = num::TryFromIntError;
183
184 #[inline]
185 fn try_from(align: usize) -> Result<Alignment, Self::Error> {
186 Self::new(align).ok_or(num::TryFromIntError(()))
187 }
188}
189
190#[unstable(feature = "ptr_alignment_type", issue = "102070")]
191impl From<Alignment> for NonZero<usize> {
192 #[inline]
193 fn from(align: Alignment) -> NonZero<usize> {
194 align.as_nonzero()
195 }
196}
197
198#[unstable(feature = "ptr_alignment_type", issue = "102070")]
199impl From<Alignment> for usize {
200 #[inline]
201 fn from(align: Alignment) -> usize {
202 align.as_usize()
203 }
204}
205
206#[unstable(feature = "ptr_alignment_type", issue = "102070")]
207impl cmp::Ord for Alignment {
208 #[inline]
209 fn cmp(&self, other: &Self) -> cmp::Ordering {
210 self.as_nonzero().get().cmp(&other.as_nonzero().get())
211 }
212}
213
214#[unstable(feature = "ptr_alignment_type", issue = "102070")]
215impl cmp::PartialOrd for Alignment {
216 #[inline]
217 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
218 Some(self.cmp(other))
219 }
220}
221
222#[unstable(feature = "ptr_alignment_type", issue = "102070")]
223impl hash::Hash for Alignment {
224 #[inline]
225 fn hash<H: hash::Hasher>(&self, state: &mut H) {
226 self.as_nonzero().hash(state)
227 }
228}
229
230#[unstable(feature = "ptr_alignment_type", issue = "102070")]
232impl Default for Alignment {
233 fn default() -> Alignment {
234 Alignment::MIN
235 }
236}
237
238#[cfg(target_pointer_width = "16")]
239#[derive(Copy, Clone, PartialEq, Eq)]
240#[repr(u16)]
241enum AlignmentEnum {
242 _Align1Shl0 = 1 << 0,
243 _Align1Shl1 = 1 << 1,
244 _Align1Shl2 = 1 << 2,
245 _Align1Shl3 = 1 << 3,
246 _Align1Shl4 = 1 << 4,
247 _Align1Shl5 = 1 << 5,
248 _Align1Shl6 = 1 << 6,
249 _Align1Shl7 = 1 << 7,
250 _Align1Shl8 = 1 << 8,
251 _Align1Shl9 = 1 << 9,
252 _Align1Shl10 = 1 << 10,
253 _Align1Shl11 = 1 << 11,
254 _Align1Shl12 = 1 << 12,
255 _Align1Shl13 = 1 << 13,
256 _Align1Shl14 = 1 << 14,
257 _Align1Shl15 = 1 << 15,
258}
259
260#[cfg(target_pointer_width = "32")]
261#[derive(Copy, Clone, PartialEq, Eq)]
262#[repr(u32)]
263enum AlignmentEnum {
264 _Align1Shl0 = 1 << 0,
265 _Align1Shl1 = 1 << 1,
266 _Align1Shl2 = 1 << 2,
267 _Align1Shl3 = 1 << 3,
268 _Align1Shl4 = 1 << 4,
269 _Align1Shl5 = 1 << 5,
270 _Align1Shl6 = 1 << 6,
271 _Align1Shl7 = 1 << 7,
272 _Align1Shl8 = 1 << 8,
273 _Align1Shl9 = 1 << 9,
274 _Align1Shl10 = 1 << 10,
275 _Align1Shl11 = 1 << 11,
276 _Align1Shl12 = 1 << 12,
277 _Align1Shl13 = 1 << 13,
278 _Align1Shl14 = 1 << 14,
279 _Align1Shl15 = 1 << 15,
280 _Align1Shl16 = 1 << 16,
281 _Align1Shl17 = 1 << 17,
282 _Align1Shl18 = 1 << 18,
283 _Align1Shl19 = 1 << 19,
284 _Align1Shl20 = 1 << 20,
285 _Align1Shl21 = 1 << 21,
286 _Align1Shl22 = 1 << 22,
287 _Align1Shl23 = 1 << 23,
288 _Align1Shl24 = 1 << 24,
289 _Align1Shl25 = 1 << 25,
290 _Align1Shl26 = 1 << 26,
291 _Align1Shl27 = 1 << 27,
292 _Align1Shl28 = 1 << 28,
293 _Align1Shl29 = 1 << 29,
294 _Align1Shl30 = 1 << 30,
295 _Align1Shl31 = 1 << 31,
296}
297
298#[cfg(target_pointer_width = "64")]
299#[derive(Copy, Clone, PartialEq, Eq)]
300#[repr(u64)]
301enum AlignmentEnum {
302 _Align1Shl0 = 1 << 0,
303 _Align1Shl1 = 1 << 1,
304 _Align1Shl2 = 1 << 2,
305 _Align1Shl3 = 1 << 3,
306 _Align1Shl4 = 1 << 4,
307 _Align1Shl5 = 1 << 5,
308 _Align1Shl6 = 1 << 6,
309 _Align1Shl7 = 1 << 7,
310 _Align1Shl8 = 1 << 8,
311 _Align1Shl9 = 1 << 9,
312 _Align1Shl10 = 1 << 10,
313 _Align1Shl11 = 1 << 11,
314 _Align1Shl12 = 1 << 12,
315 _Align1Shl13 = 1 << 13,
316 _Align1Shl14 = 1 << 14,
317 _Align1Shl15 = 1 << 15,
318 _Align1Shl16 = 1 << 16,
319 _Align1Shl17 = 1 << 17,
320 _Align1Shl18 = 1 << 18,
321 _Align1Shl19 = 1 << 19,
322 _Align1Shl20 = 1 << 20,
323 _Align1Shl21 = 1 << 21,
324 _Align1Shl22 = 1 << 22,
325 _Align1Shl23 = 1 << 23,
326 _Align1Shl24 = 1 << 24,
327 _Align1Shl25 = 1 << 25,
328 _Align1Shl26 = 1 << 26,
329 _Align1Shl27 = 1 << 27,
330 _Align1Shl28 = 1 << 28,
331 _Align1Shl29 = 1 << 29,
332 _Align1Shl30 = 1 << 30,
333 _Align1Shl31 = 1 << 31,
334 _Align1Shl32 = 1 << 32,
335 _Align1Shl33 = 1 << 33,
336 _Align1Shl34 = 1 << 34,
337 _Align1Shl35 = 1 << 35,
338 _Align1Shl36 = 1 << 36,
339 _Align1Shl37 = 1 << 37,
340 _Align1Shl38 = 1 << 38,
341 _Align1Shl39 = 1 << 39,
342 _Align1Shl40 = 1 << 40,
343 _Align1Shl41 = 1 << 41,
344 _Align1Shl42 = 1 << 42,
345 _Align1Shl43 = 1 << 43,
346 _Align1Shl44 = 1 << 44,
347 _Align1Shl45 = 1 << 45,
348 _Align1Shl46 = 1 << 46,
349 _Align1Shl47 = 1 << 47,
350 _Align1Shl48 = 1 << 48,
351 _Align1Shl49 = 1 << 49,
352 _Align1Shl50 = 1 << 50,
353 _Align1Shl51 = 1 << 51,
354 _Align1Shl52 = 1 << 52,
355 _Align1Shl53 = 1 << 53,
356 _Align1Shl54 = 1 << 54,
357 _Align1Shl55 = 1 << 55,
358 _Align1Shl56 = 1 << 56,
359 _Align1Shl57 = 1 << 57,
360 _Align1Shl58 = 1 << 58,
361 _Align1Shl59 = 1 << 59,
362 _Align1Shl60 = 1 << 60,
363 _Align1Shl61 = 1 << 61,
364 _Align1Shl62 = 1 << 62,
365 _Align1Shl63 = 1 << 63,
366}