External Publication
Visit Post

Reducing Raw Pointer Footguns: Preventing Reference Aliasing Violations at Compile Time

Rust Internals [Unofficial] May 17, 2026
Source

robofinch:

(Ah, maybe it could just be an enum in some specific scenario guaranteed to be optimized into a single pointer, a bit like how the null optimization of Option-like enums is guaranteed.)

This is exactly what I was planning – an AtomicEnum that gets packed down to a sufficiently small size that it can be stored in an atomic. This gives an enormous amount of power (e.g. it is enough to write Mutex in entirely safe code, as long as you have access to a safe futex or other comparable blocking primitive). For Mutex you just need the existing optimisation on Option, but for more complicated situations you would probably want a #[repr(…)] that's guaranteed to be able to pack multiple types of pointer into a single atomic-sized word, as long as they have sufficient alignment to allow enough of a niche.

Discussion in the ATmosphere

Loading comments...