Allow MaybeUninit union fields?

I was recently disappointed to find out that you cannot use MaybeUninit in union fields (if T: !Copy). Only Copy fields or fields wrapped in ManuallyDrop are allowed (or tuples/arrays of allowed types).

union Example<T> {
    // this is not allowed, must be ManuallyDrop<MaybeUninit<T>>
    inline: MaybeUninit<T>,
    alloc: *mut T,
}

ManuallyDrop<MaybeUninit<T>> is redundant as MaybeUninit already internally wraps T in ManuallyDrop. Nothing happens when MaybeUninit is dropped.

Could the rules be relaxed to allow MaybeUninit union fields?

Relevant Code:

1 Like

Given that you're putting it in a union already, can you say why you want to have the union field be a MaybeUninit<T> rather than a ManuallyDrop<T>?

3 Likes