Closed
Description
This function is a terror. It is full of undefined behavior, even more than it's sister, transmute
. The only uses of this function are either Undefined Behavior, are transmute
s, except that they didn't try, or are just transmute
s in a generic context.
This is unfortunate. I think we should make this function either much more useful, or deprecate it and get rid of the undefined behavior.
Changing the definition to:
unsafe fn transmute_copy<T: ?Sized, U>(t: &T) -> U {
let u: U = std::mem::uninitialized();
std::ptr::copy_nonoverlapping(t as *const t as *const u8, &mut u as *mut u as *mut u8
std::mem::size_of::<U>());
u
}
would allow us to pull types from byte arrays, for example, without having to do this all by hand.
Otherwise, it should be deprecated, because it isn't really useful enough, and leads to far too many issues; it's only stable because transmute
was unstable at the time.