pub struct UnsafeCell<T>where
T: ?Sized,{ /* private fields */ }Expand description
The core primitive for interior mutability in Rust.
If you have a reference &T, then normally in Rust the compiler performs optimizations based on
the knowledge that &T points to immutable data. Mutating that data, for example through an
alias or by transmuting a &T into a &mut T, is considered undefined behavior.
UnsafeCell<T> opts-out of the immutability guarantee for &T: a shared reference
&UnsafeCell<T> may point to data that is being mutated. This is called “interior mutability”.
All other types that allow internal mutability, such as Cell<T> and RefCell<T>, internally
use UnsafeCell to wrap their data.
Note that only the immutability guarantee for shared references is affected by UnsafeCell. The
uniqueness guarantee for mutable references is unaffected. There is no legal way to obtain
aliasing &mut, not even with UnsafeCell<T>.
UnsafeCell does nothing to avoid data races; they are still undefined behavior. If multiple
threads have access to the same UnsafeCell, they must follow the usual rules of the