|
|
@ -6,9 +6,10 @@ really can't emphasize that you should deeply think about finding Another Way
|
|
|
|
than the operations covered in this section. This is really, truly, the most
|
|
|
|
than the operations covered in this section. This is really, truly, the most
|
|
|
|
horribly unsafe thing you can do in Rust. The railguards here are dental floss.
|
|
|
|
horribly unsafe thing you can do in Rust. The railguards here are dental floss.
|
|
|
|
|
|
|
|
|
|
|
|
`mem::transmute<T, U>` takes a value of type `T` and reinterprets it to have
|
|
|
|
[`mem::transmute<T, U>`][transmute] takes a value of type `T` and reinterprets
|
|
|
|
type `U`. The only restriction is that the `T` and `U` are verified to have the
|
|
|
|
it to have type `U`. The only restriction is that the `T` and `U` are verified
|
|
|
|
same size. The ways to cause Undefined Behavior with this are mind boggling.
|
|
|
|
to have the same size. The ways to cause Undefined Behavior with this are mind
|
|
|
|
|
|
|
|
boggling.
|
|
|
|
|
|
|
|
|
|
|
|
* First and foremost, creating an instance of *any* type with an invalid state
|
|
|
|
* First and foremost, creating an instance of *any* type with an invalid state
|
|
|
|
is going to cause arbitrary chaos that can't really be predicted.
|
|
|
|
is going to cause arbitrary chaos that can't really be predicted.
|
|
|
@ -23,13 +24,16 @@ same size. The ways to cause Undefined Behavior with this are mind boggling.
|
|
|
|
* Transmuting to a reference without an explicitly provided lifetime
|
|
|
|
* Transmuting to a reference without an explicitly provided lifetime
|
|
|
|
produces an [unbounded lifetime]
|
|
|
|
produces an [unbounded lifetime]
|
|
|
|
|
|
|
|
|
|
|
|
`mem::transmute_copy<T, U>` somehow manages to be *even more* wildly unsafe than
|
|
|
|
[`mem::transmute_copy<T, U>`][transmute_copy] somehow manages to be *even more*
|
|
|
|
this. It copies `size_of<U>` bytes out of an `&T` and interprets them as a `U`.
|
|
|
|
wildly unsafe than this. It copies `size_of<U>` bytes out of an `&T` and
|
|
|
|
The size check that `mem::transmute` has is gone (as it may be valid to copy
|
|
|
|
interprets them as a `U`. The size check that `mem::transmute` has is gone (as
|
|
|
|
out a prefix), though it is Undefined Behavior for `U` to be larger than `T`.
|
|
|
|
it may be valid to copy out a prefix), though it is Undefined Behavior for `U`
|
|
|
|
|
|
|
|
to be larger than `T`.
|
|
|
|
|
|
|
|
|
|
|
|
Also of course you can get most of the functionality of these functions using
|
|
|
|
Also of course you can get most of the functionality of these functions using
|
|
|
|
pointer casts.
|
|
|
|
pointer casts.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[unbounded lifetime]: unbounded-lifetimes.html
|
|
|
|
[unbounded lifetime]: unbounded-lifetimes.html
|
|
|
|
|
|
|
|
[transmute]: ../std/mem/fn.transmute.html
|
|
|
|
|
|
|
|
[transmute_copy]: ../std/mem/fn.transmute.html
|
|
|
|