Skip to content

Implement intrinsic matching semantics of TransmuteFrom::transmute #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jswrenn opened this issue Sep 14, 2024 · 3 comments
Open

Implement intrinsic matching semantics of TransmuteFrom::transmute #18

jswrenn opened this issue Sep 14, 2024 · 3 comments

Comments

@jswrenn
Copy link
Member

jswrenn commented Sep 14, 2024

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.

@Lokathor
Copy link

it immediately occurs to me that the point of a ManuallyDrop is to prevent dropping, but it does that by being a union, so you're just sticking a union into a union... so you can just remove the ManuallyDrop layer

@RustyYato
Copy link

RustyYato commented Sep 14, 2024

@Lokathor no ManuallyDrop is not a union, and it is required for any non-Copy fields of unions.

https://doc.rust-lang.org/std/mem/struct.ManuallyDrop.html

@Lokathor
Copy link

Oh my bad I'm thinking of MaybeUninit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants