Skip to content
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

Add get_pin_ref and get_pin_mut methods to slice #78370

Closed
wants to merge 1 commit into from
Closed
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
60 changes: 60 additions & 0 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::num::NonZeroUsize;
use crate::ops::{FnMut, Range, RangeBounds};
use crate::option::Option;
use crate::option::Option::{None, Some};
use crate::pin::Pin;
use crate::ptr;
use crate::result::Result;
use crate::result::Result::{Err, Ok};
Expand Down Expand Up @@ -307,6 +308,65 @@ impl<T> [T] {
index.get_mut(self)
}

/// Returns a pinned reference to an element or subslice depending on the
/// type of index (see [`get`]) or `None` if the index is out of bounds.
///
/// [`get`]: #method.get
///
/// # Examples
///
/// ```
/// #![feature(slice_get_pin)]
/// use std::pin::Pin;
///
/// let v = vec![0, 1, 2].into_boxed_slice();
/// let pinned = Pin::from(v);
///
/// let x: Option<Pin<&i32>> = pinned.as_ref().get_pin_ref(1);
/// assert_eq!(&1, &*x.unwrap());
/// ```
#[unstable(feature = "slice_get_pin", issue = "none")]
#[inline]
pub fn get_pin_ref<I>(self: Pin<&Self>, index: I) -> Option<Pin<&I::Output>>
where
I: SliceIndex<Self>,
{
// SAFETY: `x` is guaranteed to be pinned because it comes from `self`
// which is pinned.
unsafe { self.get_ref().get(index).map(|x| Pin::new_unchecked(x)) }
}

/// Returns a pinned mutable reference to an element or subslice depending on the
/// type of index (see [`get`]) or `None` if the index is out of bounds.
///
/// [`get`]: #method.get
///
/// # Examples
///
/// ```
/// #![feature(slice_get_pin)]
/// use std::pin::Pin;
///
/// let v = vec![0, 1, 2].into_boxed_slice();
/// let mut pinned = Pin::from(v);
///
/// if let Some(mut elem) = pinned.as_mut().get_pin_mut(1) {
/// elem.set(10);
/// }
/// assert_eq!(&*pinned, &[0, 10, 2]);
/// ```
#[unstable(feature = "slice_get_pin", issue = "none")]
#[inline]
pub fn get_pin_mut<I>(self: Pin<&mut Self>, index: I) -> Option<Pin<&mut I::Output>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existence of this method means that unsafe code needs to be careful when projecting from a Pin<&mut [T]> to a &mut T (a mutable reference to one of the slice elements). It can still be done soundly, but the &mut T cannot be moved out of if the Pin<&mut [T]> is ever exposed to safe code (which could call get_pin_mut).

I think it would be good to mention this in the docs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct. Option::as_pin_mut also has the same issue. We need to add docs to both.

where
I: SliceIndex<Self>,
{
// SAFETY: `get_unchecked_mut` is never used to move the slice inside `self` (`SliceIndex`
// is sealed and all `SliceIndex::get_mut` implementations never move elements).
// `x` is guaranteed to be pinned because it comes from `self` which is pinned.
unsafe { self.get_unchecked_mut().get_mut(index).map(|x| Pin::new_unchecked(x)) }
}

/// Returns a reference to an element or subslice, without doing bounds
/// checking.
///
Expand Down