-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add functions cast::transmute_slice, cast::transmute_mut_slice #9972
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
Conversation
By adding io to the hidden std re-exports, this allows the use of println!() inside of libstd.
#[inline] | ||
pub unsafe fn transmute_slice<'a,L,G>(thing: &'a [L]) -> &'a [G] { | ||
let mut slice: Slice<G> = transmute_copy(&thing); | ||
let bytes = slice.len * mem::nonzero_size_of::<L>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment about why this can't overflow?
Also fix rand::isaac to transmute its slice properly. That didn't get updated when the slice repr changed, so it was not properly seeding itself.
I'm not convinced we should expose convenience functions for this. The usage in In C and C++, casts like this are undefined behaviour because they break the type punning rules implemented to enable type-based alias analysis |
@thestinger The problem with not exposing this is people will try it using |
After talking with @thestinger, I get the impression that his primary objection is that TBAA makes most casts like this into undefined behavior (which is why C makes it undefined). Although technically it's only accessing the resulting object type through the pointer that's undefined, and not the cast itself (at least in C). My response is twofold:
I'm also willing to restrict this to something like |
Upon further reflection, |
FWIW, I've replaced the default implementation of |
I'm going to close this out in favor of #10015. I think we should have an official way of coercing a slice to |
No description provided.