-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
mem::transmute on types of different size (but the type size is known) #47966
Comments
I doubt if we can easily fix this without pushing this E0512 error to post-monomorphization. Allowing this means the compiler has to be able to solve equations to prove that |
|
The check for equal size is pre-monomorphization (intentionally). So the compiler would have to reason about the size of the type parameter |
I think having a compiler able to solve first degree equations like that is going to be useful once we have const generics. |
I believe the correct answer here is "just use pointer casting". I don't think there's a good argument for adding complexity here when the method's use is discouraged anyway. I'm confident, however, that eventually we'll have const bounds enough to allow things like |
Triage: two years later, I think we should close this. Maybe it can be done someday, as @scottmcm points out, but it's unclear when that, if ever, will happen. |
It would be useful to have a working example of the "just use pointer casting" technique in the interim while waiting for something to be implemented. I'm arriving at this thread after writing a function which attempts to |
There is a crate which allows for this call to be made - transmute |
An an update to anyone following this, there's a group working on safe transmutes that may end up being the solution for this desire: rust-lang/lang-team#21 |
Problem
When I try to compile this method, the compiler reports that
mem::transmute
is being called on types of different sizes. (Which doesn't make sense becauseT
size should be known in this context).Compiler Error
Working Example
Replacing
T
with a struct and it compiles fine (also works withString
):Meta
The text was updated successfully, but these errors were encountered: