4 ms·
pcwalton wrote a NES emulator in Rust 7 years ago as a demonstration of Rust: https://github.com/pcwalton/sprocketnes https://github.com/pcwalton/sprocketnes A
by lilyball 7y ago
pcwalton wrote a NES emulator in Rust 7 years ago as a demonstration of Rust: https://github.com/pcwalton/sprocketnes https://github.com/pcwalton/sprocketnes
Also, a nitpick:
> One line of unsafe (std::mem::transmute::<u8>() -> i8)
You don't need unsafe for this, you can do that with an `as` cast.
- deleted 7y ago[deleted]
- swolchok 7y agoThey seem to be equivalent: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c852d2684941fe04eeb103a97bfd936b https://play.rust-lang.org/?version=stable&mode=debug&editio...
- deleted 7y ago[deleted]
- SlowRobotAhead 7y agoLike 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!
- spieglt 7y agoYeah, I'm going to fix that. I thought I'd tried `u8 as i8` and it hadn't worked, but I guess not.
- Macha 7y agoPossibly clippy complained about it and you mistook it for a compile error?