Skip to content

Implement intrinsic matching semantics of TransmuteFrom::transmute #18

@jswrenn

Description

@jswrenn

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions