TransmuteFrom models 'union transmute', which permits extensions of the trailing padding bytes of Src during the transmutation to Dst. This model is implemented by TransmuteFrom::transmute, which uses a union to perform the transmutation. Essentially:
pub unsafe fn transmute_via_union<Src, Dst>(src: Src) -> Dst {
use core::mem::ManuallyDrop;
#[repr(C)]
union Transmute<Src, Dst> {
src: ManuallyDrop<Src>,
dst: ManuallyDrop<Dst>,
}
let transmute = Transmute {
src: ManuallyDrop::new(src),
};
let dst = transmute.dst;
ManuallyDrop::into_inner(dst)
}
Unfortunately, this long-form implementation might be quite heavy on the optimizer. For example, using ManuallyDrop can cause memcpys and allocas that LLVM cannot remove: rust-lang/rust#79914
It would therefore be nice to have a intrinsic version of this that wasn't so dependent on the optimizer. We could either, as @scottmcm suggests, modify transmute_unchecked to provide these semantics, or we could provide a new intrinsic (e.g., transmute_union) that provides these semantics.
TransmuteFrommodels 'union transmute', which permits extensions of the trailing padding bytes ofSrcduring the transmutation toDst. This model is implemented byTransmuteFrom::transmute, which uses a union to perform the transmutation. Essentially:Unfortunately, this long-form implementation might be quite heavy on the optimizer. For example, using
ManuallyDropcan causememcpys andallocas that LLVM cannot remove: rust-lang/rust#79914It would therefore be nice to have a intrinsic version of this that wasn't so dependent on the optimizer. We could either, as @scottmcm suggests, modify
transmute_uncheckedto provide these semantics, or we could provide a new intrinsic (e.g.,transmute_union) that provides these semantics.