S::V.set(false); should have change the interior value, but clearly, it hasn't.
Is this a limitation of associated constants or am I doing something wrong here?
You can't mutate constants at runtime, because constants are not exist at runtime. Constants are compile-time-only thing and referring their name act like copy-pasting their declaration. So S::V.set(false); would produces same machine code as Cell::new(true).set(false), which obviously doesn't change something else.
Note that such a thing requires thread-safe (Sync) interior mutability, since such globals can be accessed and thus, potentially mutated, from multiple threads in parallel.
Simpler to transpose from Cell to AtomicCell, mental-model-wise, and relatedly, avoids the pitfall of somebody using Ordering::Relaxed and thus potentially featuring race conditions.
It's still not great, since AtomicCell is technically unsound when the type bundles padding bytes (e.g., one oughtn't use stuff like AtomicCell<(u8, u16)>).
Clippy rightfully complains about interior mutability in consts:
warning: a `const` item should never be interior mutable
--> src/main.rs:4:1
|
4 | const FOO: Cell<i32> = Cell::new(42);
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| make this a static item (maybe with lazy_static)
|
= note: `#[warn(clippy::declare_interior_mutable_const)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#declare_interior_mutable_const
warning: a `const` item with interior mutability should not be borrowed
--> src/main.rs:7:5
|
7 | FOO.set(123);
| ^^^
|
= note: `#[warn(clippy::borrow_interior_mutable_const)]` on by default
= help: assign this const to a local or static variable, and use the variable here
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_interior_mutable_const