about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJubilee <workingjubilee@gmail.com>2025-06-08 17:17:55 -0700
committerGitHub <noreply@github.com>2025-06-08 17:17:55 -0700
commitfaab0210492f8f66c0d2dcd55e495e1752bda266 (patch)
tree5582ba4a0458048ce5bc4a7f952c4b19bc32ee70
parenta5b8a45f015605fef42b7d6f35b41121f73f289c (diff)
parentbd0a81ee82bbc9e7e163bab648c86170cf816c5e (diff)
downloadrust-faab0210492f8f66c0d2dcd55e495e1752bda266.tar.gz
rust-faab0210492f8f66c0d2dcd55e495e1752bda266.zip
Rollup merge of #142162 - RalfJung:unsafe-pinned-get, r=workingjubilee,traviscross
UnsafePinned: update get() docs and signature to allow shared mutation

Follow-up to https://github.com/rust-lang/rust/pull/140638, making `get` consistent with the fact that there's an `UnsafeCell` inside this type now by returning `*mut T` instead of `*const T`.

Cc ``@rust-lang/libs-api``
Tracking issue: https://github.com/rust-lang/rust/issues/125735
-rw-r--r--library/core/src/cell.rs16
-rw-r--r--library/core/src/pin/unsafe_pinned.rs17
2 files changed, 16 insertions, 17 deletions
diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs
index ed523920e42..a4b6efe35fc 100644
--- a/library/core/src/cell.rs
+++ b/library/core/src/cell.rs
@@ -1914,6 +1914,8 @@ impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
 /// [`.get()`]: `UnsafeCell::get`
 /// [concurrent memory model]: ../sync/atomic/index.html#memory-model-for-atomic-accesses
 ///
+/// # 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
@@ -2167,10 +2169,9 @@ impl<T: ?Sized> UnsafeCell<T> {
 
     /// Gets a mutable pointer to the wrapped value.
     ///
-    /// This can be cast to a pointer of any kind.
-    /// Ensure that the access is unique (no active references, mutable or not)
-    /// when casting to `&mut T`, and ensure that there are no mutations
-    /// or mutable aliases going on when casting to `&T`
+    /// This can be cast to a pointer of any kind. When creating references, you must uphold the
+    /// aliasing rules; see [the type-level docs][UnsafeCell#aliasing-rules] for more discussion and
+    /// caveats.
     ///
     /// # Examples
     ///
@@ -2219,10 +2220,9 @@ impl<T: ?Sized> UnsafeCell<T> {
     /// The difference from [`get`] is that this function accepts a raw pointer,
     /// which is useful to avoid the creation of temporary references.
     ///
-    /// The result can be cast to a pointer of any kind.
-    /// Ensure that the access is unique (no active references, mutable or not)
-    /// when casting to `&mut T`, and ensure that there are no mutations
-    /// or mutable aliases going on when casting to `&T`.
+    /// This can be cast to a pointer of any kind. When creating references, you must uphold the
+    /// aliasing rules; see [the type-level docs][UnsafeCell#aliasing-rules] for more discussion and
+    /// caveats.
     ///
     /// [`get`]: UnsafeCell::get()
     ///
diff --git a/library/core/src/pin/unsafe_pinned.rs b/library/core/src/pin/unsafe_pinned.rs
index dbcceb807ab..17f7bcd306b 100644
--- a/library/core/src/pin/unsafe_pinned.rs
+++ b/library/core/src/pin/unsafe_pinned.rs
@@ -86,13 +86,12 @@ impl<T: ?Sized> UnsafePinned<T> {
         ptr::from_mut(self) as *mut T
     }
 
-    /// Get read-only access to the contents of a shared `UnsafePinned`.
+    /// Get mutable access to the contents of a shared `UnsafePinned`.
     ///
-    /// Note that `&UnsafePinned<T>` is read-only if `&T` is read-only. This means that if there is
-    /// mutation of the `T`, future reads from the `*const T` returned here are UB! Use
-    /// [`UnsafeCell`] if you also need interior mutability.
+    /// This can be cast to a pointer of any kind. When creating references, you must uphold the
+    /// aliasing rules; see [`UnsafeCell`] for more discussion and caveats.
     ///
-    /// [`UnsafeCell`]: crate::cell::UnsafeCell
+    /// [`UnsafeCell`]: crate::cell::UnsafeCell#aliasing-rules
     ///
     /// ```rust,no_run
     /// #![feature(unsafe_pinned)]
@@ -100,16 +99,16 @@ impl<T: ?Sized> UnsafePinned<T> {
     ///
     /// unsafe {
     ///     let mut x = UnsafePinned::new(0);
-    ///     let ptr = x.get(); // read-only pointer, assumes immutability
+    ///     let ptr = x.get();
     ///     x.get_mut_unchecked().write(1);
-    ///     ptr.read(); // UB!
+    ///     assert_eq!(ptr.read(), 1);
     /// }
     /// ```
     #[inline(always)]
     #[must_use]
     #[unstable(feature = "unsafe_pinned", issue = "125735")]
-    pub const fn get(&self) -> *const T {
-        ptr::from_ref(self) as *const T
+    pub const fn get(&self) -> *mut T {
+        self.value.get()
     }
 
     /// Gets an immutable pointer to the wrapped value.