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