rustc_data_structures/aligned.rs
1use std::ptr::Alignment;
2
3use rustc_serialize::PointeeSized;
4
5/// Returns the ABI-required minimum alignment of a type in bytes.
6///
7/// This is equivalent to [`align_of`], but also works for some unsized
8/// types (e.g. slices or rustc's `List`s).
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 [`align_of::<Self>()`], for unsized types it depends on the type, for
19/// example `[T]` has alignment of `T`.
20///
21/// [`align_of::<Self>()`]: align_of
22pub unsafe trait Aligned: PointeeSized {
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}