Re:Implementing a NES Emulator in Rust
by bluejekyll on Tuesday March 19, 2019 @01:04PM
(#19432814)
Hm, I’m not sure I see the difference between those concepts, but I do like your framing better.
[ Parent
| on HN ]
The main point is that if you take safe code and put it inside an unsafe block, the meaning of the code doesn't change. So for example, array indexing is bounds-checked in safe code, and non-bounds-checked array access is only available in unsafe blocks. This does not mean that the call `a[i]` loses bounds-checking in an unsafe block. It has the same semantics regardless of context. Rather, the separate call `a.get_unchecked(i)` is available inside an unsafe block and cannot be called in safe code.
Re:Implementing a NES Emulator in Rust
by steveklabnik on Tuesday March 19, 2019 @01:26PM
(#19433080)
What my sibling said, but also, that unsafe doesn’t mean you’re free from rules; where safe and unsafe interact, your unsafe code is expected to uphold the safety guarantees that the safe code expects.
This matters because of stuff like the article; if you see the other thread, I’m pretty sure (though haven’t verified yet) that this code has UB, even though they’re using unsafe.
Re:Implementing a NES Emulator in Rust
by bluejekyll on Tuesday March 19, 2019 @02:22PM
(#19433862)
Right, which is why I phrased it as a “few restrictions”, I do understand the point you’re making.
I suppose the reason I consider it restrictions is due to things like “in safe code you can not work with raw pointers”. That seems like a restriction to me, but I can see why saying that way is less accurate.