|
|
Subscribe / Log in / New account

transmute

transmute

Posted Jul 12, 2022 11:00 UTC (Tue) by tialaramex (subscriber, #21167)
In reply to: EXCELLENT! by pbonzini
Parent article: Rust frontend approved for GCC

Note that transmute notionally does memcpy, and memcpy is exactly how you're allowed (without Undefined Behaviour) to write type punning in C or C++. In most cases the compiler can see what you're getting at and elide the actual memcpy but if for whatever reason the compiler can't figure out what's really going on, that memcpy prevents you getting into a place where unrelated optimisations blow up your transmute.

Transmute is hideously dangerous *anyway* but not directly because of aliasing concerns. For example you can transmute some bytes you found into a string, which Rust then assumes is UTF-8 encoded. Or you can transmute the integer 0 into a NonZeroUsize, and then hand that to APIs which explicitly took NonZeroUsize because they can't handle zeroes. Or transmute 69 into a bool which is now sometimes neither equal to true nor false. Nice. Wait, no, that's not nice at all.


to post comments

transmute

Posted Jul 12, 2022 13:42 UTC (Tue) by nix (subscriber, #2304) [Link]

Or transmute 69 into a bool which is now sometimes neither equal to true nor false. Nice. Wait, no, that's not nice at all.
Sounds like the perfect thing to use to encode SQL NULL :P


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds