After reading how unsafe rust is relient on the state entablished by the safe rust, I started to think about ways to represent this reliency.
With traits one can mark them unsafe, which means that to be able to implement them you need to use unsafe again.
So why donāt we allow usage unsafe keyword before structs fields?
struct A {
unsafe size: usize,
}
This would make it to require that all access to the field needs to be within unsafe block.
With this you could clearly mark what parts of a struct are relied upon inside unsafe blocks.
Open questions
First I thought it would be enough to limit write access of unsafe variables to unsafe blocks, because modification is only way to break invariants, but then I thought about interior mutability. Is interior mutability really a problem?
Unsafe code can still rely non-unsafe variables, because its optin nature. It also requires programmer to add quite noisy incantation to the variable. So would people actually use this and would it actually be useful at all?