rustc_data_structures/
aligned.rs

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