I’d like to transmute a MaybeUninit<T> to a T.
use std::mem::transmute;
use std::mem::MaybeUninit;
fn make_init<T: Sized>(mt: MaybeUninit<T>) -> T {
unsafe { transmute(mt) }
}
fn main() {
make_init(MaybeUninit::new(1));
}
However this program gives me:
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
What’s the right way to do this?
>Solution :
Generally, the workaround when the compiler doesn’t know that types have the same size is to use transmute_copy(), but in your case there is MaybeUninit::assume_init() that does exactly what you want.