I am building a compiler intermediate representation similar to the one in librustc/mir/repr.rs
, but my Instruction
type needs to have some shared fields, so it looks like this:
struct Instruction {
opcode: u32,
result_type: u8,
payload: Payload,
}
enum Payload {
Unary { arg: u32 },
Binary { args: [u32; 2] },
Other(Box<MoreData>),
}
struct MoreData { ... }
I want this type to be 16 bytes large, but the Payload
discriminator gets in the way. It is stored inside the Payload
type, doubling its size from 8 bytes to 16. Is there any way of storing the Payload
discriminator in the padding after result_type
to keep the whole Instruction
type at 16 bytes?
Related to this, the Box<MoreData>
is normally just an 8-byte pointer, but if MoreData
is a dynamically sized type, it doubles in size because it also stores the MoreData
size. Is there any way of storing the size of a dynamically sized type inside the object itself so references don’t double in size?