@@ -1811,7 +1811,36 @@ impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
1811
1811
///
1812
1812
/// [`.get_mut()`]: `UnsafeCell::get_mut`
1813
1813
///
1814
- /// `UnsafeCell<T>` has the same in-memory representation as its inner type `T`.
1814
+ /// `UnsafeCell<T>` has the same in-memory representation as its inner type `T`. A consequence
1815
+ /// of this guarantee is that it is possible to convert between `T` and `UnsafeCell<T>`.
1816
+ /// However, it is only valid to obtain a `*mut T` pointer or `&mut T` reference to the
1817
+ /// contents of an `UnsafeCell<T>` through [`.get()`], [`.raw_get()`] or [`.get_mut()`], e.g.:
1818
+ ///
1819
+ /// ```rust
1820
+ /// use std::cell::UnsafeCell;
1821
+ ///
1822
+ /// let mut x: UnsafeCell<u32> = UnsafeCell::new(5);
1823
+ /// let p1: &UnsafeCell<u32> = &x;
1824
+ /// // using `.get()` is okay:
1825
+ /// unsafe {
1826
+ /// // SAFETY: there exist no other references to the contents of `x`
1827
+ /// let p2: &mut u32 = &mut *p1.get();
1828
+ /// };
1829
+ /// // using `.raw_get()` is also okay:
1830
+ /// unsafe {
1831
+ /// // SAFETY: there exist no other references to the contents of `x` in this scope
1832
+ /// let p2: &mut u32 = &mut *UnsafeCell::raw_get(p1 as *const _);
1833
+ /// };
1834
+ /// // using `.get_mut()` is always safe:
1835
+ /// let p2: &mut u32 = x.get_mut();
1836
+ /// // but the following is not allowed!
1837
+ /// // let p2: &mut u32 = unsafe {
1838
+ /// // let t: *mut u32 = &x as *const _ as *mut u32;
1839
+ /// // &mut *t
1840
+ /// // };
1841
+ /// ```
1842
+ ///
1843
+ /// [`.raw_get()`]: `UnsafeCell::raw_get`
1815
1844
///
1816
1845
/// # Examples
1817
1846
///
0 commit comments