2 ms·
> You still have to cast &[T] into &[MaybeUninit<T>] somehow. unsafe{ std::mem::transmute(slice) } This is probably the only way that will ever exist, be
by gpm 2y ago
> You still have to cast &[T] into &[MaybeUninit<T>] somehow.
unsafe{ std::mem::transmute(slice) }
This is probably the only way that will ever exist, because
let slice: &mut [NonZeroU8] = ...;
let slice_uninit: &mut [MaybeUninit<NonZeroU8>] = ...;
let nonzero_uninit: &mut MaybeUninit<NonZeroU8> = &mut slice_uninit[0];
*nonzero_uninit = MaybeUninit::zeroed();
slice[0]; // Undefined behavior for sure by now.
Is all safe except for the cast.
I.e. MaybeUninit<T> allows you to write invalid bit-patterns to T, so you can't safely cast a reference to T to it (and if you do unsafely cast a reference to T to it you can't soundly write an invalid bit pattern). All current forms of safely making a MaybeUninit take ownership of the value they are declaring to be MaybeUninit for this reason.
I guess at some point we might get methods for this on types that can take on all bit patterns - if/when that's encoded as a trait.