const CELL_ALIGNMENT: usize = std::mem::align_of::<EntryType>();
#[repr(align(CELL_ALIGNMENT))]
pub struct AlignedCell;
#[repr(C)]
pub struct AlignedHeader {
_align: [AlignedCell; 0],
count: usize,
}
CELL_ALIGNMENT是一个常量。但看起来repr
不允许常量。只允许使用文字。有什么办法绕过这个吗?
否,不能在#[repr(align(_))]
中使用表达式。它必须是字面意思。
接下来最好的事情是:您可以在编译时使用静态断言板条箱:来断言您的结构与另一个结构的对齐
#[repr(align(8))]
pub struct AlignedCell;
static_assertions::assert_eq_align!(AlignedCell, EntryType);
它不会自动设置对齐方式,但如果对齐方式错误,则会出现编译器错误。