use core::mem::align_of;
pub unsafe trait Aligned {
const ALIGNMENT: usize;
}
unsafe impl<T> Aligned for &T {
const ALIGNMENT: usize = align_of::<T>();
}
unsafe impl<T> Aligned for &mut T {
const ALIGNMENT: usize = align_of::<T>();
}
unsafe impl<T> Aligned for Option<&T> {
const ALIGNMENT: usize = align_of::<T>();
}
unsafe impl<T> Aligned for Option<&mut T> {
const ALIGNMENT: usize = align_of::<T>();
}
#[cfg(feature = "alloc")]
mod alloc_impl {
use super::*;
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::sync::Arc;
const fn max(a: usize, b: usize) -> usize {
if a > b {
a
} else {
b
}
}
unsafe impl<T> Aligned for Box<T> {
const ALIGNMENT: usize = align_of::<T>();
}
unsafe impl<T> Aligned for Rc<T> {
const ALIGNMENT: usize = max(align_of::<T>(), align_of::<usize>());
}
unsafe impl<T> Aligned for Arc<T> {
const ALIGNMENT: usize = max(align_of::<T>(), align_of::<usize>());
}
unsafe impl<T> Aligned for Option<Box<T>> {
const ALIGNMENT: usize = align_of::<T>();
}
unsafe impl<T> Aligned for Option<Rc<T>> {
const ALIGNMENT: usize = max(align_of::<T>(), align_of::<usize>());
}
unsafe impl<T> Aligned for Option<Arc<T>> {
const ALIGNMENT: usize = max(align_of::<T>(), align_of::<usize>());
}
}