can't transmute array of MaybeUninit<T> to [T; _] for generic T
I ran into tihs error and don't know how to solve it:
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> src/path/to/my/code.rs:155:50
|
155 | let xs: [T; 16] = unsafe { std::mem::transmute(xs) };
| ^^^^^^^^^^^^^^^^^^^
|
= note: source type: `[MaybeUninit<T>; 16]` (size can vary because of T)
= note: target type: `[T; 16]` (size can vary because of T)
In this instance, I was converting code that previously created an fixed-size array of a specific type to instead create an array of a parameterized type, which has a trait defined in my crate. (The trait has several associated types, and the impl has several where clauses relating to those associated types).
In other words, this was just working a second ago, but now that it's a parameterized type, it won't work. I put a Sized requirement on the trait and the relevant associated types, but to no avail.
Apologies I can't share more of the code. Any ideas on what the cause of this is? It is my second time using MaybeUninit for the purposes of filling in a fixed size array. In this case I switched the code to use Vec, and got around the issue. It's not critical to use a fixed size array in my case, but I generally prefer it for various reasons.
6 comments · 87% upvoted · other discussions
mem::transmutehas some known limitations involving arrays of generic types. You can work around this by using raw pointers instead:jstrong [OP] 3 points · Dec 2020 · shipyard.rs
this worked, thank you!