In the example given for MaybeUnint, it is valid to std::mem::transmute() an [MaybeUninit<Vec<u32>>; 1000]
to an [Vec<u32>; 1000]
.
This is a lot like what I need to accomplish, except in my case it's a transmute from [MaybeUninit<T>; LEN]
to [T; LEN]
for varying LEN
(currently 0 ..= 32
).
The problem is that the compiler rejects this code:
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> deltoid/src/arrays.rs:43:29
|
43 | Ok(unsafe { mem::transmute::<_, Self>(new) })
| ^^^^^^^^^^^^^^^^^^^^^^^^^
...
132 | impl_traits_for_array!(length: 0, using: Array0Delta);
| -------------------------------------------------------- in this macro invocation
|
= note: source type: `[std::mem::MaybeUninit<T>; 0]` (size can vary because of T)
= note: target type: `[T; 0]` (size can vary because of T)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
From the error I infer that the reason it rejects it is that T
is a generic parameter, and not because of the array length.
But any time the code is actually executed, the type parameter will long since have been substituted for a concrete type. And even if it wasn't, the compiler should still be able to infer that the T
in the source and target types are in fact the same. So why would the compiler have to reject that?
EDIT: I also know it can't be because of any Sized
constraints, because bounding T: Sized
does not compile either.