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