Beingessner: Rust's Unsafe Pointer Types Need An Overhaul
Beingessner: Rust's Unsafe Pointer Types Need An Overhaul
Posted Mar 24, 2022 21:56 UTC (Thu) by NYKevin (subscriber, #129325)In reply to: Beingessner: Rust's Unsafe Pointer Types Need An Overhaul by excors
Parent article: Beingessner: Rust's Unsafe Pointer Types Need An Overhaul
Safe Rust already has a notion of "some bit patterns are not used by this type" and can therefore do "clever" things like packing different types together to make a type-safe untagged union. This has all of the advantages of a tagged union or Option/Either type, including compile-time type checking, but with no (or very little) overhead. Most of the time, you can and should try to make do with that, because you want the compiler to yell at you if you forget a case. From the perspective of unsafe Rust, even this might not be flexible enough, and so there does need to be an escape hatch where you can smuggle pointers around in other values, to some extent. But as the article acknowledges, the rules for this are a great deal more restrictive than just "make sure you end up with the same physical bit pattern you started with" - CHERI will trap if you try to do that, and Rust isn't really in a position to prevent CHERI from doing that in the general case.
