Skip to content

What to do with transmute_copy #1703

Closed
Closed
@strega-nil

Description

@strega-nil

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 transmutes, except that they didn't try, or are just transmutes 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions