4 ms·
One runtime panic I wish was a compile-time error in the rust standard library is the use of an incorrect memory order, eg Ordering::Release with AtomicBool::lo
by ComputerGuru 4y ago
One runtime panic I wish was a compile-time error in the rust standard library is the use of an incorrect memory order, eg Ordering::Release with AtomicBool::load().
It would have been fairly trivial to set up generic constraints specifying if a read, write, or read-write ordering semantic is expected and to fail to compile if it wasn’t met.
- nynx 4y agoThis is going to happen at some point now that const generics arrived.
- ComputerGuru 4y agoLikely only once we finally get const parameters specified as regular arguments. It doesn’t need const at all, though. You just need three traits and either first class enum variants as types or a pseudo enum (mod/struct Ordering with ZST structs Acquire, Release, etc)
- pitaj 4y agoThis could be a good application of a Clippy lint.
- oconnor663 4y agoI think the reason it's not a compile-time error is that it's actually possible to select your ordering at runtime, like this: use std::sync::atomic::*; let x = AtomicU64::new(0); let ordering = if rand::random() { Ordering::Relaxed } else { Ordering::SeqCst }; x.fetch_add(1, ordering);
- ComputerGuru 4y agoYes, I tried (a few months ago) mocking a PR for this that refactored the enum variants into ZST structs and that was the only sticking point for backwards compatibility.