rust-lang / libc

Raw bindings to platform APIs for Rust
https://docs.rs/libc
Apache License 2.0
2.1k stars 1.04k forks source link

Wrapper for volatile fields #4130

Open tgross35 opened 5 days ago

tgross35 commented 5 days ago

In C, fields can be defined as volatile whereas for Rust it is the accesses. I'm wondering if we should consider wrapping any volatile members in some UnsafeCell-based wrapper to ensure they aren't read by accident.

Unfortunately this is a bit tricky since we can't allow it to be moved by the compiler.

tgross35 commented 4 days ago

I'm thinking maybe we should provide a struct Volatile<T>(UnsafeCell<T>); wrapper type that at least makes it clear what these fields are, even if we can't actually enforce volatility.