Implement tag union discriminant extraction for byte- and newtype-variants

This commit is contained in:
Ayaz Hafiz 2022-10-04 18:04:46 -05:00
parent 00ca8f2f80
commit 251b3865d9
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
2 changed files with 87 additions and 41 deletions

View file

@ -879,15 +879,8 @@ impl<'a> UnionLayout<'a> {
}
}
pub fn tag_id_layout(&self) -> Layout<'a> {
// TODO is it beneficial to return a more specific layout?
// e.g. Layout::bool() and Layout::VOID
match self.discriminant() {
Discriminant::U0 => Layout::u8(),
Discriminant::U1 => Layout::u8(),
Discriminant::U8 => Layout::u8(),
Discriminant::U16 => Layout::u16(),
}
pub fn tag_id_layout(&self) -> Layout<'static> {
self.discriminant().layout()
}
fn stores_tag_id_in_pointer_bits(tags: &[&[Layout<'a>]], target_info: TargetInfo) -> bool {
@ -1138,6 +1131,17 @@ impl Discriminant {
pub const fn alignment_bytes(&self) -> u32 {
self.stack_size()
}
pub const fn layout(&self) -> Layout<'static> {
// TODO is it beneficial to return a more specific layout?
// e.g. Layout::bool() and Layout::VOID
match self {
Discriminant::U0 => Layout::u8(),
Discriminant::U1 => Layout::u8(),
Discriminant::U8 => Layout::u8(),
Discriminant::U16 => Layout::u16(),
}
}
}
/// Custom type so we can get the numeric representation of a symbol in tests (so `#UserApp.3`
@ -2713,7 +2717,7 @@ impl<'a> Layout<'a> {
Layout::Builtin(Builtin::Int(IntWidth::U8))
}
pub fn u16() -> Layout<'a> {
pub const fn u16() -> Layout<'a> {
Layout::Builtin(Builtin::Int(IntWidth::U16))
}