1
Fork 0

Support enum variants in offset_of!

This commit is contained in:
George Bateman 2023-08-15 20:10:45 +01:00
parent 9d83ac2179
commit e936416a8d
No known key found for this signature in database
GPG key ID: C417AA9C4039EFCF
27 changed files with 472 additions and 75 deletions

View file

@ -250,7 +250,7 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
Ty::is_transparent(self)
}
pub fn offset_of_subfield<C>(self, cx: &C, indices: impl Iterator<Item = usize>) -> Size
pub fn offset_of_subfield<C>(self, cx: &C, indices: impl Iterator<Item = OffsetOfIdx>) -> Size
where
Ty: TyAbiInterface<'a, C>,
{
@ -258,13 +258,21 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
let mut offset = Size::ZERO;
for index in indices {
offset += layout.fields.offset(index);
layout = layout.field(cx, index);
assert!(
layout.is_sized(),
"offset of unsized field (type {:?}) cannot be computed statically",
layout.ty
);
match index {
OffsetOfIdx::Field(field) => {
let index = field.index();
offset += layout.fields.offset(index);
layout = layout.field(cx, index);
assert!(
layout.is_sized(),
"offset of unsized field (type {:?}) cannot be computed statically",
layout.ty
);
}
OffsetOfIdx::Variant(variant) => {
layout = layout.for_variant(cx, variant);
}
}
}
offset