Skip to content

mem::transmute() docs could use some additional detailΒ #44281

Closed
@wesleywiser

Description

@wesleywiser

Here's a few things I thought of while reading the docs:

  • The term type punning is used without being defined. Ordinarily, I wouldn't complain about this because it's very easy to Google search but most of the search results on the first page say that type punning is undefined behavior (in C\C++). This doesn't appear to be true in Rust because the Undefined Behaviors Page only references LLVM's pointer aliasing rules which seem to indicate that TBAA doesn't apply.
    • It would probably be a good idea to give a short definition of the term and state that it is (or is not) undefined behavior in Rust.
~~~  - Having an explicit reason stated as to why it's UB would help the reader figure out if the same rule applies to what they are trying to do.~~~

- Another common use of type punning in C is to take a set of bytes (perhaps read off disk or the network) and then cast them into a `struct` of the same size. 
  - I think this is an allowed usage of `transmute()` provided the struct is `repr(C)`. Having an example that shows how to do this correctly and also discusses the potential issues such as relying on the endianness being the same would be helpful. If this isn't allowed, then stating that would also be useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.P-mediumMedium priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions