Re-interpreting a Repr-C'd struct's bytes as a certain type

Uhhhhh, guys?

You don't need an UnsafeCell if you have a *const T or *mut T.


Wait a second, I misread the code I linked. What the heck!?!?!

unsafe impl<T: ?Sized> Freeze for *const T {}
unsafe impl<T: ?Sized> Freeze for *mut T {}

That... that can't be right!

@cuviper how did you construct those playground tests before? I'm having a difficult time getting the functions to not be eliminated as dead code. (I wish the playground had a --lib mode...).
(edit: never mind, I finally spotted the playground link in your post)

I may need to read up on LLVM's noalias. Perhaps it only extends to data accessed through an offset of the pointer itself, and not to data accessed through another layer of indirection.

2 Likes