4 ms·
An unsafe block is significantly better than the code proposed in the article.
by nmilo 4y ago
An unsafe block is significantly better than the code proposed in the article.
- justin_oaks 4y agoI know some people get upset when unsafe is used when there's a "safe" alternative, but unsafe isn't too bad in test code, is it? The concerns of undefined behavior, data races, use-after-free, etc. seem a lot less consequential in test code.
- nmilo 4y agoUnsafe isn't too bad in any code. The point of unsafe is that it is a declaration that the programmer has verified the code is safe. It is certainly easier to verify something along the lines of: unsafe { transmute::<u32, Error>(error_code) } than it is to verify that the weird macro code in the article is correct. There is no data races, use-after-free, etc. in that block, it does exactly what the article needs. To me, Rust programmers seem to have a weird notion that just the word "unsafe" is enough to conjure demons and monsters into otherwise totally normal code.
- Nullabillity 4y agoYou'd think so, but that code is far into nasal demon territory. Both because the default byte representation of an enum isn't stable, and because an out-of-bounds enum value (wherever it comes from) is always UB.