Skip to main content

core/mem/
alignment.rs

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/// A type storing a `usize` which is a power of two, and thus
9/// represents a possible alignment in the Rust abstract machine.
10///
11/// Note that particularly large alignments, while representable in this type,
12/// are likely not to be supported by actual allocators and linkers.
13#[unstable(feature = "ptr_alignment_type", issue = "102070")]
14#[derive(Copy)]
15#[derive_const(Clone, PartialEq, Eq)]
16#[repr(transparent)]
17pub struct Alignment {
18    // This field is never used directly (nor is the enum),
19    // as it's just there to convey the validity invariant.
20    // (Hopefully it'll eventually be a pattern type instead.)
21    _inner_repr_trick: AlignmentEnum,
22}
23
24// Alignment is `repr(usize)`, but via extra steps.
25const _: () = assert!(size_of::<Alignment>() == size_of::<usize>());
26const _: () = assert!(align_of::<Alignment>() == align_of::<usize>());
27
28fn _alignment_can_be_structurally_matched(a: Alignment) -> bool {
29    matches!(a, Alignment::MIN)
30}
31
32impl Alignment {
33    /// The smallest possible alignment, 1.
34    ///
35    /// All addresses are always aligned at least this much.
36    ///
37    /// # Examples
38    ///
39    /// ```
40    /// #![feature(ptr_alignment_type)]
41    /// use std::mem::Alignment;
42    ///
43    /// assert_eq!(Alignment::MIN.as_usize(), 1);
44    /// ```
45    #[unstable(feature = "ptr_alignment_type", issue = "102070")]
46    pub const MIN: Self = Self::new(1).unwrap();
47
48    /// Returns the alignment for a type.
49    ///
50    /// This provides the same numerical value as [`align_of`],
51    /// but in an `Alignment` instead of a `usize`.
52    #[unstable(feature = "ptr_alignment_type", issue = "102070")]
53    #[inline]
54    #[must_use]
55    pub const fn of<T>() -> Self {
56        <T as mem::SizedTypeProperties>::ALIGNMENT
57    }
58
59    /// Returns the [ABI]-required minimum alignment of the type of the value that `val` points to.
60    ///
61    /// This function is identical to [`Alignment::of::<T>()`][Self::of] whenever
62    /// <code>T: [Sized]</code>,
63    /// but also supports determining the alignment required by a `dyn Trait` value, which is the
64    /// alignment of the underlying concrete type.
65    ///
66    /// This provides the same numerical value as [`align_of_val`],
67    /// but in an `Alignment` instead of a `usize`.
68    ///
69    /// [ABI]: https://en.wikipedia.org/wiki/Application_binary_interface
70    ///
71    /// # Examples
72    ///
73    /// ```
74    /// #![feature(ptr_alignment_type)]
75    /// use std::mem::Alignment;
76    ///
77    /// assert_eq!(Alignment::of_val(&5i32).as_usize(), 4);
78    /// ```
79    ///
80    /// (Caution: [it is not guaranteed][type-layout] that the alignment of `i32` is `4`;
81    /// that is, the above assertion does not pass on all platforms.)
82    ///
83    /// `dyn` types may have different alignments for different values;
84    /// `Alignment::of_val()` can be used to learn those alignments:
85    ///
86    /// ```
87    /// #![feature(ptr_alignment_type)]
88    /// use std::mem::Alignment;
89    ///
90    /// let a: &dyn ToString = &1234u16;
91    /// let b: &dyn ToString = &String::from("abcd");
92    ///
93    /// assert_eq!(Alignment::of_val(a), Alignment::of::<u16>());
94    /// assert_eq!(Alignment::of_val(b), Alignment::of::<String>());
95    /// ```
96    ///
97    /// [type-layout]: ../../reference/type-layout.html#r-layout.primitive
98    #[inline]
99    #[must_use]
100    #[unstable(feature = "ptr_alignment_type", issue = "102070")]
101    pub const fn of_val<T: MetaSized>(val: &T) -> Self {
102        let align = mem::align_of_val(val);
103        // SAFETY: `align_of_val` returns valid alignment
104        unsafe { Alignment::new_unchecked(align) }
105    }
106
107    /// Returns the [ABI]-required minimum alignment of the type of the value that `val` points to.
108    ///
109    /// This function is identical to [`Alignment::of_val()`], except that it can be used with raw
110    /// pointers in situations where it would be unsound or undesirable to convert them to
111    /// [`&` references][primitive@reference] and impose the aliasing rules that come with that.
112    ///
113    /// [ABI]: https://en.wikipedia.org/wiki/Application_binary_interface
114    ///
115    /// # Safety
116    ///
117    /// This function is only safe to call if the following conditions hold:
118    ///
119    /// - If `T` is `Sized`, this function is always safe to call.
120    /// - If the unsized tail of `T` is:
121    ///     - a [slice], then the length of the slice tail must be an initialized
122    ///       integer, and the size of the *entire value*
123    ///       (dynamic tail length + statically sized prefix) must fit in `isize`.
124    ///       For the special case where the dynamic tail length is 0, this function
125    ///       is safe to call.
126    ///     - a [trait object], then the vtable part of the pointer must point
127    ///       to a valid vtable acquired by an unsizing coercion, and the size
128    ///       of the *entire value* (dynamic tail length + statically sized prefix)
129    ///       must fit in `isize`.
130    ///     - an (unstable) [extern type], then this function is always safe to
131    ///       call, but may panic or otherwise return the wrong value, as the
132    ///       extern type's layout is not known. This is the same behavior as
133    ///       [`Alignment::of_val`] on a reference to a type with an extern type tail.
134    ///     - otherwise, it is conservatively not allowed to call this function.
135    ///
136    /// [trait object]: ../../book/ch17-02-trait-objects.html
137    /// [extern type]: ../../unstable-book/language-features/extern-types.html
138    ///
139    /// # Examples
140    ///
141    /// ```
142    /// #![feature(ptr_alignment_type)]
143    /// use std::mem::Alignment;
144    ///
145    /// assert_eq!(unsafe { Alignment::of_val_raw(&5i32) }.as_usize(), 4);
146    /// ```
147    ///
148    /// (Caution: [it is not guaranteed][type-layout] that the alignment of `i32` is `4`;
149    /// that is, the above assertion does not pass on all platforms.)
150    ///
151    /// [type-layout]: ../../reference/type-layout.html#r-layout.primitive
152    #[inline]
153    #[must_use]
154    #[unstable(feature = "ptr_alignment_type", issue = "102070")]
155    pub const unsafe fn of_val_raw<T: MetaSized>(val: *const T) -> Self {
156        // SAFETY: precondition propagated to the caller
157        let align = unsafe { mem::align_of_val_raw(val) };
158        // SAFETY: `align_of_val_raw` returns valid alignment
159        unsafe { Alignment::new_unchecked(align) }
160    }
161
162    /// Creates an `Alignment` from a `usize`, or returns `None` if it's
163    /// not a power of two.
164    ///
165    /// Note that `0` is not a power of two, nor a valid alignment.
166    #[unstable(feature = "ptr_alignment_type", issue = "102070")]
167    #[inline]
168    pub const fn new(align: usize) -> Option<Self> {
169        if align.is_power_of_two() {
170            // SAFETY: Just checked it only has one bit set
171            Some(unsafe { Self::new_unchecked(align) })
172        } else {
173            None
174        }
175    }
176
177    /// Creates an `Alignment` from a power-of-two `usize`.
178    ///
179    /// # Safety
180    ///
181    /// `align` must be a power of two.
182    ///
183    /// Equivalently, it must be `1 << exp` for some `exp` in `0..usize::BITS`.
184    /// It must *not* be zero.
185    #[unstable(feature = "ptr_alignment_type", issue = "102070")]
186    #[inline]
187    #[track_caller]
188    pub const unsafe fn new_unchecked(align: usize) -> Self {
189        assert_unsafe_precondition!(
190            check_language_ub,
191            "Alignment::new_unchecked requires a power of two",
192            (align: usize = align) => align.is_power_of_two()
193        );
194
195        // SAFETY: By precondition, this must be a power of two, and
196        // our variants encompass all possible powers of two.
197        unsafe { mem::transmute::<usize, Alignment>(align) }
198    }
199
200    /// Returns the alignment as a [`usize`].
201    #[unstable(feature = "ptr_alignment_type", issue = "102070")]
202    #[inline]
203    pub const fn as_usize(self) -> usize {
204        // Going through `as_nonzero_usize` helps this be more clearly the inverse of
205        // `new_unchecked`, letting MIR optimizations fold it away.
206
207        self.as_nonzero_usize().get()
208    }
209
210    /// Returns the alignment as a <code>[NonZero]<[usize]></code>.
211    #[unstable(feature = "ptr_alignment_type", issue = "102070")]
212    #[deprecated(
213        since = "1.96.0",
214        note = "renamed to `as_nonzero_usize`",
215        suggestion = "as_nonzero_usize"
216    )]
217    #[inline]
218    pub const fn as_nonzero(self) -> NonZero<usize> {
219        self.as_nonzero_usize()
220    }
221
222    /// Returns the alignment as a <code>[NonZero]<[usize]></code>.
223    #[unstable(feature = "ptr_alignment_type", issue = "102070")]
224    #[inline]
225    pub const fn as_nonzero_usize(self) -> NonZero<usize> {
226        // This transmutes directly to avoid the UbCheck in `NonZero::new_unchecked`
227        // since there's no way for the user to trip that check anyway -- the
228        // validity invariant of the type would have to have been broken earlier --
229        // and emitting it in an otherwise simple method is bad for compile time.
230
231        // SAFETY: All the discriminants are non-zero.
232        unsafe { mem::transmute::<Alignment, NonZero<usize>>(self) }
233    }
234
235    /// Returns the base-2 logarithm of the alignment.
236    ///
237    /// This is always exact, as `self` represents a power of two.
238    ///
239    /// # Examples
240    ///
241    /// ```
242    /// #![feature(ptr_alignment_type)]
243    /// use std::ptr::Alignment;
244    ///
245    /// assert_eq!(Alignment::of::<u8>().log2(), 0);
246    /// assert_eq!(Alignment::new(1024).unwrap().log2(), 10);
247    /// ```
248    #[unstable(feature = "ptr_alignment_type", issue = "102070")]
249    #[inline]
250    pub const fn log2(self) -> u32 {
251        self.as_nonzero_usize().trailing_zeros()
252    }
253
254    /// Returns a bit mask that can be used to match this alignment.
255    ///
256    /// This is equivalent to `!(self.as_usize() - 1)`.
257    ///
258    /// # Examples
259    ///
260    /// ```
261    /// #![feature(ptr_mask)]
262    /// #![feature(ptr_alignment_type)]
263    /// use std::mem::Alignment;
264    /// use std::ptr::NonNull;
265    ///
266    /// #[repr(align(1))] struct Align1(u8);
267    /// #[repr(align(2))] struct Align2(u16);
268    /// #[repr(align(4))] struct Align4(u32);
269    /// let one = <NonNull<Align1>>::dangling().as_ptr();
270    /// let two = <NonNull<Align2>>::dangling().as_ptr();
271    /// let four = <NonNull<Align4>>::dangling().as_ptr();
272    ///
273    /// assert_eq!(four.mask(Alignment::of::<Align1>().mask()), four);
274    /// assert_eq!(four.mask(Alignment::of::<Align2>().mask()), four);
275    /// assert_eq!(four.mask(Alignment::of::<Align4>().mask()), four);
276    /// assert_ne!(one.mask(Alignment::of::<Align4>().mask()), one);
277    /// ```
278    #[unstable(feature = "ptr_alignment_type", issue = "102070")]
279    #[inline]
280    pub const fn mask(self) -> usize {
281        // SAFETY: The alignment is always nonzero, and therefore decrementing won't overflow.
282        !(unsafe { self.as_usize().unchecked_sub(1) })
283    }
284
285    // FIXME(const-hack) Remove me once `Ord::max` is usable in const
286    pub(crate) const fn max(a: Self, b: Self) -> Self {
287        if a.as_usize() > b.as_usize() { a } else { b }
288    }
289}
290
291#[unstable(feature = "ptr_alignment_type", issue = "102070")]
292impl fmt::Debug for Alignment {
293    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
294        write!(f, "{:?} (1 << {:?})", self.as_nonzero_usize(), self.log2())
295    }
296}
297
298#[unstable(feature = "ptr_alignment_type", issue = "102070")]
299#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
300const impl TryFrom<NonZero<usize>> for Alignment {
301    type Error = num::TryFromIntError;
302
303    #[inline]
304    fn try_from(align: NonZero<usize>) -> Result<Alignment, Self::Error> {
305        align.get().try_into()
306    }
307}
308
309#[unstable(feature = "ptr_alignment_type", issue = "102070")]
310#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
311const impl TryFrom<usize> for Alignment {
312    type Error = num::TryFromIntError;
313
314    #[inline]
315    fn try_from(align: usize) -> Result<Alignment, Self::Error> {
316        Self::new(align).ok_or(num::TryFromIntError(num::IntErrorKind::NotAPowerOfTwo))
317    }
318}
319
320#[unstable(feature = "ptr_alignment_type", issue = "102070")]
321#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
322const impl From<Alignment> for NonZero<usize> {
323    #[inline]
324    fn from(align: Alignment) -> NonZero<usize> {
325        align.as_nonzero_usize()
326    }
327}
328
329#[unstable(feature = "ptr_alignment_type", issue = "102070")]
330#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
331const impl From<Alignment> for usize {
332    #[inline]
333    fn from(align: Alignment) -> usize {
334        align.as_usize()
335    }
336}
337
338#[unstable(feature = "ptr_alignment_type", issue = "102070")]
339#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
340const impl cmp::Ord for Alignment {
341    #[inline]
342    fn cmp(&self, other: &Self) -> cmp::Ordering {
343        self.as_nonzero_usize().cmp(&other.as_nonzero_usize())
344    }
345}
346
347#[unstable(feature = "ptr_alignment_type", issue = "102070")]
348#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
349const impl cmp::PartialOrd for Alignment {
350    #[inline]
351    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
352        Some(self.cmp(other))
353    }
354}
355
356#[unstable(feature = "ptr_alignment_type", issue = "102070")]
357impl hash::Hash for Alignment {
358    #[inline]
359    fn hash<H: hash::Hasher>(&self, state: &mut H) {
360        self.as_nonzero_usize().hash(state)
361    }
362}
363
364/// Returns [`Alignment::MIN`], which is valid for any type.
365#[unstable(feature = "ptr_alignment_type", issue = "102070")]
366#[rustc_const_unstable(feature = "const_default", issue = "143894")]
367const impl Default for Alignment {
368    fn default() -> Alignment {
369        Alignment::MIN
370    }
371}
372
373#[cfg(target_pointer_width = "16")]
374#[derive(Copy)]
375#[derive_const(Clone, PartialEq, Eq)]
376#[repr(usize)]
377enum AlignmentEnum {
378    _Align1Shl0 = 1 << 0,
379    _Align1Shl1 = 1 << 1,
380    _Align1Shl2 = 1 << 2,
381    _Align1Shl3 = 1 << 3,
382    _Align1Shl4 = 1 << 4,
383    _Align1Shl5 = 1 << 5,
384    _Align1Shl6 = 1 << 6,
385    _Align1Shl7 = 1 << 7,
386    _Align1Shl8 = 1 << 8,
387    _Align1Shl9 = 1 << 9,
388    _Align1Shl10 = 1 << 10,
389    _Align1Shl11 = 1 << 11,
390    _Align1Shl12 = 1 << 12,
391    _Align1Shl13 = 1 << 13,
392    _Align1Shl14 = 1 << 14,
393    _Align1Shl15 = 1 << 15,
394}
395
396#[cfg(target_pointer_width = "32")]
397#[derive(Copy)]
398#[derive_const(Clone, PartialEq, Eq)]
399#[repr(usize)]
400enum AlignmentEnum {
401    _Align1Shl0 = 1 << 0,
402    _Align1Shl1 = 1 << 1,
403    _Align1Shl2 = 1 << 2,
404    _Align1Shl3 = 1 << 3,
405    _Align1Shl4 = 1 << 4,
406    _Align1Shl5 = 1 << 5,
407    _Align1Shl6 = 1 << 6,
408    _Align1Shl7 = 1 << 7,
409    _Align1Shl8 = 1 << 8,
410    _Align1Shl9 = 1 << 9,
411    _Align1Shl10 = 1 << 10,
412    _Align1Shl11 = 1 << 11,
413    _Align1Shl12 = 1 << 12,
414    _Align1Shl13 = 1 << 13,
415    _Align1Shl14 = 1 << 14,
416    _Align1Shl15 = 1 << 15,
417    _Align1Shl16 = 1 << 16,
418    _Align1Shl17 = 1 << 17,
419    _Align1Shl18 = 1 << 18,
420    _Align1Shl19 = 1 << 19,
421    _Align1Shl20 = 1 << 20,
422    _Align1Shl21 = 1 << 21,
423    _Align1Shl22 = 1 << 22,
424    _Align1Shl23 = 1 << 23,
425    _Align1Shl24 = 1 << 24,
426    _Align1Shl25 = 1 << 25,
427    _Align1Shl26 = 1 << 26,
428    _Align1Shl27 = 1 << 27,
429    _Align1Shl28 = 1 << 28,
430    _Align1Shl29 = 1 << 29,
431    _Align1Shl30 = 1 << 30,
432    _Align1Shl31 = 1 << 31,
433}
434
435#[cfg(target_pointer_width = "64")]
436#[derive(Copy)]
437#[derive_const(Clone, PartialEq, Eq)]
438#[repr(usize)]
439enum AlignmentEnum {
440    _Align1Shl0 = 1 << 0,
441    _Align1Shl1 = 1 << 1,
442    _Align1Shl2 = 1 << 2,
443    _Align1Shl3 = 1 << 3,
444    _Align1Shl4 = 1 << 4,
445    _Align1Shl5 = 1 << 5,
446    _Align1Shl6 = 1 << 6,
447    _Align1Shl7 = 1 << 7,
448    _Align1Shl8 = 1 << 8,
449    _Align1Shl9 = 1 << 9,
450    _Align1Shl10 = 1 << 10,
451    _Align1Shl11 = 1 << 11,
452    _Align1Shl12 = 1 << 12,
453    _Align1Shl13 = 1 << 13,
454    _Align1Shl14 = 1 << 14,
455    _Align1Shl15 = 1 << 15,
456    _Align1Shl16 = 1 << 16,
457    _Align1Shl17 = 1 << 17,
458    _Align1Shl18 = 1 << 18,
459    _Align1Shl19 = 1 << 19,
460    _Align1Shl20 = 1 << 20,
461    _Align1Shl21 = 1 << 21,
462    _Align1Shl22 = 1 << 22,
463    _Align1Shl23 = 1 << 23,
464    _Align1Shl24 = 1 << 24,
465    _Align1Shl25 = 1 << 25,
466    _Align1Shl26 = 1 << 26,
467    _Align1Shl27 = 1 << 27,
468    _Align1Shl28 = 1 << 28,
469    _Align1Shl29 = 1 << 29,
470    _Align1Shl30 = 1 << 30,
471    _Align1Shl31 = 1 << 31,
472    _Align1Shl32 = 1 << 32,
473    _Align1Shl33 = 1 << 33,
474    _Align1Shl34 = 1 << 34,
475    _Align1Shl35 = 1 << 35,
476    _Align1Shl36 = 1 << 36,
477    _Align1Shl37 = 1 << 37,
478    _Align1Shl38 = 1 << 38,
479    _Align1Shl39 = 1 << 39,
480    _Align1Shl40 = 1 << 40,
481    _Align1Shl41 = 1 << 41,
482    _Align1Shl42 = 1 << 42,
483    _Align1Shl43 = 1 << 43,
484    _Align1Shl44 = 1 << 44,
485    _Align1Shl45 = 1 << 45,
486    _Align1Shl46 = 1 << 46,
487    _Align1Shl47 = 1 << 47,
488    _Align1Shl48 = 1 << 48,
489    _Align1Shl49 = 1 << 49,
490    _Align1Shl50 = 1 << 50,
491    _Align1Shl51 = 1 << 51,
492    _Align1Shl52 = 1 << 52,
493    _Align1Shl53 = 1 << 53,
494    _Align1Shl54 = 1 << 54,
495    _Align1Shl55 = 1 << 55,
496    _Align1Shl56 = 1 << 56,
497    _Align1Shl57 = 1 << 57,
498    _Align1Shl58 = 1 << 58,
499    _Align1Shl59 = 1 << 59,
500    _Align1Shl60 = 1 << 60,
501    _Align1Shl61 = 1 << 61,
502    _Align1Shl62 = 1 << 62,
503    _Align1Shl63 = 1 << 63,
504}