about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2025-06-07 15:42:19 +0200
committerRalf Jung <post@ralfj.de>2025-06-07 15:43:15 +0200
commitbafe406711a4e7e9e2683d9245fcc13f306e1d83 (patch)
treed79fa47aa200a6ba71043e0a5b4a0c968c196a25
parent321dde12528a6baf0990ec611d33122b68c33fca (diff)
downloadrust-bafe406711a4e7e9e2683d9245fcc13f306e1d83.tar.gz
rust-bafe406711a4e7e9e2683d9245fcc13f306e1d83.zip
UnsafePinned: update get() docs and signature to allow shared mutation
-rw-r--r--library/core/src/cell.rs2
-rw-r--r--library/core/src/pin/unsafe_pinned.rs19
2 files changed, 12 insertions, 9 deletions
diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs
index ed523920e42..0656ab98ee3 100644
--- a/library/core/src/cell.rs
+++ b/library/core/src/cell.rs
@@ -2170,7 +2170,7 @@ impl<T: ?Sized> UnsafeCell<T> {
     /// 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`
+    /// or mutable aliases going on when casting to `&T`.
     ///
     /// # Examples
     ///
diff --git a/library/core/src/pin/unsafe_pinned.rs b/library/core/src/pin/unsafe_pinned.rs
index dbcceb807ab..f3d5e079838 100644
--- a/library/core/src/pin/unsafe_pinned.rs
+++ b/library/core/src/pin/unsafe_pinned.rs
@@ -86,11 +86,14 @@ 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.
+    /// 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`.
+    ///
+    /// All the usual caveats around mutation shared state apply, see [`UnsafeCell`].
     ///
     /// [`UnsafeCell`]: crate::cell::UnsafeCell
     ///
@@ -100,16 +103,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.