3 ms·
Like the that your hobby might be opening Rust code, searching unsafe just for the thrills :) Edit: Jesus kids, learn a little chill it was a joke. Someone did
by SlowRobotAhead 7y ago
Like the that your hobby might be opening Rust code, searching unsafe just for the thrills :)
Edit: Jesus kids, learn a little chill it was a joke. Someone didn’t like this so much they went through and downvoted all my other posts they could. Wow... that’s real serious.
- nevi-me 7y agoIt's a quote from TFA
- zellyn 7y agoIt was mentioned in the README, and stood out to me as surprising (surely you don't need unsafe to cast from unsigned to signed bytes?)
- slunk 7y agoWithout knowing enough about Rust to speak authoritatively... u8 can represent larger integers than i8 (no sign bit). Surely it's not completely safe.
- zellyn 7y agoHmmm. I was assuming this was just changing interpretations of the same underlying 8 bits. No? Edit: yeah, “Casting between two integers of the same size (e.g. i32 -> u32) is a no-op” (https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics https://doc.rust-lang.org/reference/expressions/operator-exp...)
- slunk 7y agoYup, fair enough.
- p4lindromica 7y ago`mem::transmute` is roughly equivalent to a `reinterpret_cast` in C++. It treats the bits of a u8 as an i8. In the Rust definition of safety (mutable xor shared, no data races, memory safety, etc), treating the bits of a u8 as an i8 is safe and can be done with an `as` cast.
- slunk 7y agoThanks for clarifying that for me!