Skip to content

UnsafePinned: update get() docs and signature to allow shared mutation #142162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -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()
///
Expand Down
17 changes: 8 additions & 9 deletions library/core/src/pin/unsafe_pinned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,29 @@ 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)]
/// use std::pin::UnsafePinned;
///
/// 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.
Expand Down
Loading