I believe it would be technically feasible (easy, even) to have unions with at most one unsized variant.
Unsizing should be safe:
-
T -> dyn U: The vtable contains 2 values relevant for the unsized union:
- The alignment: This is stored directly in the vtable of T, accessing it does not access the data pointer
- The size: Same.
Note that drop_in_place is not relevant because unions do not drop their contents.
-
[T; N] -> [T]: The metadata consists of the array length which is statically known and does not require accessing the variant.
@eddyb: In the last paragraph here you wrote that there is a problem with unsized unions but I haven't been able to figure it out. Maybe you were talking about unions with more than one unsized variant?
I believe it would be technically feasible (easy, even) to have unions with at most one unsized variant.
Unsizing should be safe:
T -> dyn U: The vtable contains 2 values relevant for the unsized union:Note that
drop_in_placeis not relevant because unions do not drop their contents.[T; N] -> [T]: The metadata consists of the array length which is statically known and does not require accessing the variant.@eddyb: In the last paragraph here you wrote that there is a problem with unsized unions but I haven't been able to figure it out. Maybe you were talking about unions with more than one unsized variant?