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. As explained below, for the duration
of the lifetime of an &mut, no other reference may exist and no pointer may be used to access
that memory; this applies 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
concurrent memory model: conflicting non-synchronized accesses must be done via the APIs in
core::sync::atomic.
The UnsafeCell API itself is technically very simple: .get() gives you a raw pointer
*mut T to its contents. It is up to you as the abstraction designer to use that raw pointer
correctly.
§Aliasing rules
The precise Rust aliasing rules are somewhat in flux, but the main points are not contentious:
-
If you create a safe reference with lifetime 'a (either a &T or &mut T reference), then
you must not access the data in any way that contradicts that reference for the remainder of
'a, and you must not create any contradicting references. For example, this means that if
you take the *mut T from an UnsafeCell<T> and cast it to a &T, then the data in T must
remain immutable (modulo any UnsafeCell data found within T, of course) until that
reference’s lifetime expires, and no &mut reference to this data may be created. Similarly,
if you create a &mut T reference, then you must not access the data within the UnsafeCell
with any other pointer/reference until that reference expires, and no reference of any kind
may be created.
-
For both &T without UnsafeCell<_> and &mut T, you must also not deallocate the data
until the reference expires. As a special exception, given a &T, any part of it that is
inside an UnsafeCell<_> may be deallocated during the lifetime of the reference, after the
last time the reference is used (dereferenced or reborrowed). Since you cannot deallocate a part
of what a reference points to, this means the memory a &T points to can be deallocated only if
every part of it (including padding) is inside an UnsafeCell.
However, whenever a &UnsafeCell<T> is constructed or dereferenced, it must still point to
live memory and the compiler is allowed to insert spurious reads if it can prove that this
memory has not yet been deallocated.
To assist with proper design, the following scenarios are explicitly declared legal
for single-threaded code:
-
A &T reference can be released to safe code and there it can co-exist with other &T
references, but not with a &mut T
-
A &mut T reference may be released to safe code provided neither other &mut T nor &T
co-exist with it. A &mut T must always be unique.
Note that whilst mutating the contents of a &UnsafeCell<T> (even while other
&UnsafeCell<T> references alias the cell) is
ok (provided you enforce the above invariants some other way), it is still undefined behavior
to have aliasing &mut UnsafeCell<T> (or aliasing &mut of any type). That is, UnsafeCell is a wrapper
designed to have a special interaction with shared accesses (i.e., through an
&UnsafeCell<_> reference); there is no magic whatsoever when dealing with exclusive
accesses (e.g., through a &mut UnsafeCell<_>): neither the cell nor the wrapped value
may be aliased for the duration of that &mut borrow.
This is showcased by the .get_mut() accessor, which is a safe getter that yields
a &mut T.
§Memory layout
UnsafeCell<T> has the same in-memory representation as its inner type T. A consequence
of this guarantee is that it is possible to convert between T and UnsafeCell<T>.
Special care has to be taken when converting a nested T inside of an Outer<T> type
to an Outer<UnsafeCell<T>> type: this is not sound when the Outer<T> type enables niche
optimizations. For example, the type Option<NonNull<u8>> is typically 8 bytes large on
64-bit platforms, but the type Option<UnsafeCell<NonNull<u8>>> takes up 16 bytes of space.
Therefore this is not a valid conversion, despite NonNull<u8> and UnsafeCell<NonNull<u8>>>
having the same memory layout. This is because UnsafeCell disables niche optimizations in
order to avoid its interior mutability property from spreading from T into the Outer type,
thus this can cause distortions in the type size in these cases.
The following examples make use of this guarantee:
/// # Safety
/// The caller must not call `get_mut_unchecked` again (on any alias of `ptr`) for the duration
/// of the lifetime of the returned reference.
unsafe fn get_mut_unchecked<T>(ptr: &UnsafeCell<T>) -> &mut T {
let t = ptr as *const UnsafeCell<T> as *mut T;
unsafe { &mut *t }
}
fn get_shared<T>(ptr: &mut T) -> &UnsafeCell<T> {
let t = ptr as *mut T as *const UnsafeCell<T>;
// SAFETY: `T` and `UnsafeCell<T>` have the same memory layout
unsafe { &*t }
}
§Examples
Here is an example showcasing how to soundly mutate the contents of an UnsafeCell<_> despite
there being multiple references aliasing the cell:
use std::cell::UnsafeCell;
let x: UnsafeCell<i32> = 42.into();
// Get multiple / concurrent / shared references to the same `x`.
let (p1, p2): (&UnsafeCell<i32>, &UnsafeCell<i32>) = (&x, &x);
unsafe {
// SAFETY: within this scope there are no other references to `x`'s contents,
// so ours is effectively unique.
let p1_exclusive: &mut i32 = &mut *p1.get(); // -- borrow --+
*p1_exclusive += 27; // |
} // <---------- cannot go beyond this point -------------------+
unsafe {
// SAFETY: within this scope nobody expects to have exclusive access to `x`'s contents,
// so we can have multiple shared accesses concurrently.
let p2_shared: &i32 = &*p2.get();
assert_eq!(*p2_shared, 42 + 27);
let p1_shared: &i32 = &*p1.get();
assert_eq!(*p1_shared, *p2_shared);
}
The following example showcases the fact that exclusive access to an UnsafeCell<T>
implies exclusive access to its T:
#![forbid(unsafe_code)]
// with exclusive accesses, `UnsafeCell` is a transparent no-op wrapper, so no need for
// `unsafe` here.
use std::cell::UnsafeCell;
let mut x: UnsafeCell<i32> = 42.into();
// Get a compile-time-checked unique reference to `x`.
let p_unique: &mut UnsafeCell<i32> = &mut x;
// With an exclusive reference, we can mutate the contents for free.
*p_unique.get_mut() = 0;
// Or, equivalently:
x = UnsafeCell::new(0);
// When we own the value, we can extract the contents for free.
let contents: i32 = x.into_inner();
assert_eq!(contents, 0);