Hacker News new | ask | show | jobs
by steveklabnik 1604 days ago
The docs for as_mut_ptr: https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.h...

> Incorrect usage of this method:

  let mut x = MaybeUninit::<Vec<u32>>::uninit();
  let x_vec = unsafe { &mut *x.as_mut_ptr() };
  // We have created a reference to an uninitialized vector! This is undefined behavior.
Also, above, it explicitly describes the intended API for partially initializing a struct: https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.h...
1 comments

Thanks Steve!

It turns out I had misremembered; the cast I was thinking of is

https://github.com/oxidecomputer/hubris/blob/master/sys/kern...

from &mut MaybeUninit<[T]> to &mut [MaybeUninit<T>], which doesn't construct a reference to something uninitialized.