5 ms·
Yikes. This is not well-written Rust; code like the below actually defeats the purpose of using Rust, and without any specific reason for doing so. I personal
by pizza234 4y ago
Yikes.
This is not well-written Rust; code like the below actually defeats the purpose of using Rust, and without any specific reason for doing so.
I personally discourage people from using this software.
static mut KEYBOARD_HOOKED: bool = false;
fn start_keyboard_hook(&self) {
if unsafe { KEYBOARD_HOOKED } {
return;
}
}
The build even requires an assembler (NASM), which is odd, in this context.
- mwcampbell 4y agoI disagree. Not all Rust code needs to be maximally generalized and reusable. If this were library code, I might feel differently. But here we're looking at code for installing a global, system-wide keyboard hook. Using a global variable is a direct, pragmatic, no-nonsense way of doing it. Edit: Further, there's no such thing as "the purpose of using Rust". Different users can use the same tool for different purposes, and Rust is no different.
- pizza234 4y agoThat's fair, but I seriously consider the danger of a slippery slope. To keep in mind, on a pragmatic level, that this type of global can be trivially implemented, at a minimum, via atomics, so the cost to avoid the unsafe is near-zero. Atomics are not supported by all the platforms, but based on my understanding of their targets (x86-64), they're supported.
- FreeFull 4y agoAt the very least, I would have used an AtomicBool rather than a static mut. See https://github.com/rust-lang/rust/issues/53639 https://github.com/rust-lang/rust/issues/53639 . As a bonus, AtomicBool doesn't require any unsafe code.
- staticassertion 4y agoIt's not that big of a deal tbh and I'm actually not sure there's even a practical safety issue, though there could be bugs if the compiler makes some optimizations based on that (unlikely imo). AFAIK on any typical platform there's no way you'd have "tearing" for a single byte ie: this will never store an invalid boolean representation.