Skip to content

Commit 46b495c

Browse files
authored
Unrolled build for #142162
Rollup merge of #142162 - RalfJung:unsafe-pinned-get, r=workingjubilee,traviscross UnsafePinned: update get() docs and signature to allow shared mutation Follow-up to #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: #125735
2 parents c31cccb + bd0a81e commit 46b495c

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

library/core/src/cell.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,8 @@ impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
19141914
/// [`.get()`]: `UnsafeCell::get`
19151915
/// [concurrent memory model]: ../sync/atomic/index.html#memory-model-for-atomic-accesses
19161916
///
1917+
/// # Aliasing rules
1918+
///
19171919
/// The precise Rust aliasing rules are somewhat in flux, but the main points are not contentious:
19181920
///
19191921
/// - 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> {
21672169

21682170
/// Gets a mutable pointer to the wrapped value.
21692171
///
2170-
/// This can be cast to a pointer of any kind.
2171-
/// Ensure that the access is unique (no active references, mutable or not)
2172-
/// when casting to `&mut T`, and ensure that there are no mutations
2173-
/// or mutable aliases going on when casting to `&T`
2172+
/// This can be cast to a pointer of any kind. When creating references, you must uphold the
2173+
/// aliasing rules; see [the type-level docs][UnsafeCell#aliasing-rules] for more discussion and
2174+
/// caveats.
21742175
///
21752176
/// # Examples
21762177
///
@@ -2219,10 +2220,9 @@ impl<T: ?Sized> UnsafeCell<T> {
22192220
/// The difference from [`get`] is that this function accepts a raw pointer,
22202221
/// which is useful to avoid the creation of temporary references.
22212222
///
2222-
/// The result can be cast to a pointer of any kind.
2223-
/// Ensure that the access is unique (no active references, mutable or not)
2224-
/// when casting to `&mut T`, and ensure that there are no mutations
2225-
/// or mutable aliases going on when casting to `&T`.
2223+
/// This can be cast to a pointer of any kind. When creating references, you must uphold the
2224+
/// aliasing rules; see [the type-level docs][UnsafeCell#aliasing-rules] for more discussion and
2225+
/// caveats.
22262226
///
22272227
/// [`get`]: UnsafeCell::get()
22282228
///

library/core/src/pin/unsafe_pinned.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,29 @@ 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. When creating references, you must uphold the
92+
/// aliasing rules; see [`UnsafeCell`] for more discussion and caveats.
9493
///
95-
/// [`UnsafeCell`]: crate::cell::UnsafeCell
94+
/// [`UnsafeCell`]: crate::cell::UnsafeCell#aliasing-rules
9695
///
9796
/// ```rust,no_run
9897
/// #![feature(unsafe_pinned)]
9998
/// use std::pin::UnsafePinned;
10099
///
101100
/// unsafe {
102101
/// let mut x = UnsafePinned::new(0);
103-
/// let ptr = x.get(); // read-only pointer, assumes immutability
102+
/// let ptr = x.get();
104103
/// x.get_mut_unchecked().write(1);
105-
/// ptr.read(); // UB!
104+
/// assert_eq!(ptr.read(), 1);
106105
/// }
107106
/// ```
108107
#[inline(always)]
109108
#[must_use]
110109
#[unstable(feature = "unsafe_pinned", issue = "125735")]
111-
pub const fn get(&self) -> *const T {
112-
ptr::from_ref(self) as *const T
110+
pub const fn get(&self) -> *mut T {
111+
self.value.get()
113112
}
114113

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

0 commit comments

Comments
 (0)