-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Confusing documentation on mem::transmute #64073
Comments
I don't see the difference. We do guarantee that |
I was not aware this is guaranteed to work, and it's not explicitly called out in the snippet. So it's not UB, but the clarity can probably be improved. |
I think this is less "UB" and more "unspecified behavior":
Exactly. PRs welcome. :D The docs should probably also mention what came up in rust-lang/rust-clippy#4484: if you do this at home, make sure the size and alignment of both types is the same, because otherwise the allocator will be unhappy with you. |
Documentation on mem::transmute presents the following piece of code:
The problem is it's never explained why the first described solution is Undefined Behavior. This is confusing.
Second, the use of transmute fromThis is actually guaranteed to be safe, but that's not explicitly called out in the snippet. Perhaps transmuting&i32
toOption<&i32>
which is inherently unsafe is not very illustrative.NonZeroU8
tou8
or possiblychar
tou32
would be a better example.Finally, the proposed "correct" way of doing this seems to actually contain undefined behavior:
std::mem::forget(v_orig);
is called only after the new vector is created, and until it's called there are two mutable references to the backing storage ofv_orig
.The text was updated successfully, but these errors were encountered: