-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[Question] Can we guarantee that the memory representation of a newtype will be equal to the representation of the type it wraps? #32146
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
Comments
C ABIs don't guarantee this btw, aggregates are treated differently than the types they contain, in general, but it depends on the types (I believe a newtype of a pointer will work in all the ABIs we support?). For more information, see all the |
CC #29702. |
Just for completeness, a Drop impl on the |
C has the guarantee that you can cast between a pointer-to-struct and a pointer to the type of the first field of the struct. That would be useful. |
/cc @rust-lang/lang |
This is part of the bigger question of what kind of ABI guarantees we are providing. We should probably consider that question as a whole rather than providing a bunch of smaller, ad hoc, and poorly documented guarantees. |
See rust-lang/rfcs#1758 for an RFC proposing a repr for this, I believe. |
Triage: This can be closed, as it's answered with "use the |
To clarify, given
struct Foo<T>(T);
, can we make the guarantee thatFoo<T>
will have the same memory representation asT
?I ask this as I have run into a couple of occasions where I would like to
std::mem::transmute
a slice ofFoo<T>
to a slice ofT
in some hot code.I performed some tests and
std::mem::size_of
seems to consistently showFoo<T>
to have the same size asT
. The code below passes (playpen link).This leaves me wondering, what else might stand in the way? Is the use of
std::mem::transmute
I suggest above a bad idea? If so, why? Any input appreciated!The text was updated successfully, but these errors were encountered: