I´m writing a VM in Rust and I have a C and C++ background. I need union-like functionality because on the VM stack I can either store an int
or a float
.
In C I had a union:
union stack_record_t {
int i;
float f;
};
I can use the record as int
or as float
with zero runtime overhead. I have a static bytecode analyzer which will find type errors before the bytecode executes, so I don't have to store a flag alongside the record.
I don´t know if it is a good idea to use unions in Rust because they are unsafe. Is there any safe way to do this in Rust - also with zero cost? Should I just use the unsafe Rust unions?