4 ms·
It 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?)
by zellyn 7y ago
It 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!