Skip to main content

rustc_data_structures/
aligned.rs

1use std::marker::PointeeSized;
2#[cfg(not(bootstrap))]
3use std::mem::Alignment;
4#[cfg(bootstrap)]
5use std::ptr::Alignment;
6
7/// Returns the ABI-required minimum alignment of a type in bytes.
8///
9/// This is equivalent to [`align_of`], but also works for some unsized
10/// types (e.g. slices or rustc's `List`s).
11pub const fn align_of<T: ?Sized + Aligned>() -> Alignment {
12    T::ALIGN
13}
14
15/// A type with a statically known alignment.
16///
17/// # Safety
18///
19/// `Self::ALIGN` must be equal to the alignment of `Self`. For sized types it
20/// is [`align_of::<Self>()`], for unsized types it depends on the type, for
21/// example `[T]` has alignment of `T`.
22///
23/// [`align_of::<Self>()`]: align_of
24pub unsafe trait Aligned: PointeeSized {
25    /// Alignment of `Self`.
26    const ALIGN: Alignment;
27}
28
29unsafe impl<T> Aligned for T {
30    const ALIGN: Alignment = Alignment::of::<Self>();
31}
32
33unsafe impl<T> Aligned for [T] {
34    const ALIGN: Alignment = Alignment::of::<T>();
35}