Skip to content

Commit bafe406

Browse files
committed
UnsafePinned: update get() docs and signature to allow shared mutation
1 parent 321dde1 commit bafe406

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

library/core/src/cell.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2170,7 +2170,7 @@ impl<T: ?Sized> UnsafeCell<T> {
21702170
/// This can be cast to a pointer of any kind.
21712171
/// Ensure that the access is unique (no active references, mutable or not)
21722172
/// when casting to `&mut T`, and ensure that there are no mutations
2173-
/// or mutable aliases going on when casting to `&T`
2173+
/// or mutable aliases going on when casting to `&T`.
21742174
///
21752175
/// # Examples
21762176
///

library/core/src/pin/unsafe_pinned.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,14 @@ impl<T: ?Sized> UnsafePinned<T> {
8686
ptr::from_mut(self) as *mut T
8787
}
8888

89-
/// Get read-only access to the contents of a shared `UnsafePinned`.
89+
/// Get mutable access to the contents of a shared `UnsafePinned`.
9090
///
91-
/// Note that `&UnsafePinned<T>` is read-only if `&T` is read-only. This means that if there is
92-
/// mutation of the `T`, future reads from the `*const T` returned here are UB! Use
93-
/// [`UnsafeCell`] if you also need interior mutability.
91+
/// This can be cast to a pointer of any kind.
92+
/// Ensure that the access is unique (no active references, mutable or not)
93+
/// when casting to `&mut T`, and ensure that there are no mutations
94+
/// or mutable aliases going on when casting to `&T`.
95+
///
96+
/// All the usual caveats around mutation shared state apply, see [`UnsafeCell`].
9497
///
9598
/// [`UnsafeCell`]: crate::cell::UnsafeCell
9699
///
@@ -100,16 +103,16 @@ impl<T: ?Sized> UnsafePinned<T> {
100103
///
101104
/// unsafe {
102105
/// let mut x = UnsafePinned::new(0);
103-
/// let ptr = x.get(); // read-only pointer, assumes immutability
106+
/// let ptr = x.get();
104107
/// x.get_mut_unchecked().write(1);
105-
/// ptr.read(); // UB!
108+
/// assert_eq!(ptr.read(), 1);
106109
/// }
107110
/// ```
108111
#[inline(always)]
109112
#[must_use]
110113
#[unstable(feature = "unsafe_pinned", issue = "125735")]
111-
pub const fn get(&self) -> *const T {
112-
ptr::from_ref(self) as *const T
114+
pub const fn get(&self) -> *mut T {
115+
self.value.get()
113116
}
114117

115118
/// Gets an immutable pointer to the wrapped value.

0 commit comments

Comments
 (0)