-
Notifications
You must be signed in to change notification settings - Fork 59
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
Can the layouts of Vec<i32>
and Vec<u32>
guaranteed to be the same?
#560
Comments
Vec<i32>
and Vec<u32>
guaranteed to be the same?Vec<i32>
and Vec<u32>
guaranteed to be the same?
In current rust this is not the case, but it definitely should be added eventually. It's an extremely handy property. |
I believe it's currently the case that One could imagine that LTO could determine some field is accessed a lot more than others and should be laid out first, and that it could make a different determination for different generic instantiations. This isn't to say that we shouldn't provide this guarantee, just that there are real reasons why it really shouldn't be assumed. |
That is certainly an intersting use case, I had not considered that LTO and/or PGO could influence field ordering. |
The intended way to convert I'll close the issue since the question has not been answered and as a feature request, this should be directed at t-libs-api. |
I have a rather interesting use case in which I have to expose the Vec to the user and modify it in place with type erased code, so unfortunately I can't use those methods. It looks like I'll have to keep the extra variants for Thank you everyone for your comments and insights. |
Currently, the layouts of
Vec<A>
andVec<B>
have no guarantees. It would be useful in some cases to know the layout ofVec<A>
for some A is the same asVec<B>
. For my usecase the exact field order doesn't matter, just that they could be the same as another type from within the same compilation.Specifically, for any two types
T
andU
, if their layout and alignment are the exact same, all bit patterns are valid for both, and areFreeze
andUnpin
, can&mut Vec<T>
be transmuted to&mut Vec<U>
and pushed to without immediate UB?If such layout guarantees of Vec do not exist, can we expect them to exist at a later date?
Example
I would also like to ask the question for Option, but Vec seems like it could achieve such a guarantee more easily because its data is behind a pointer.
The text was updated successfully, but these errors were encountered: