Skip to content

Commit 3bcb85e

Browse files
PinMut: Add safe get_mut and rename unsafe fns to get_mut_unchecked and map_unchecked
1 parent 56e8f29 commit 3bcb85e

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

src/libcore/mem.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,12 @@ impl<'a, T: ?Sized + Unpin> PinMut<'a, T> {
11191119
pub fn new(reference: &'a mut T) -> PinMut<'a, T> {
11201120
PinMut { inner: reference }
11211121
}
1122+
1123+
/// Get a mutable reference to the data inside of this `PinMut`.
1124+
#[unstable(feature = "pin", issue = "49150")]
1125+
pub fn get_mut(this: PinMut<'a, T>) -> &'a mut T {
1126+
this.inner
1127+
}
11221128
}
11231129

11241130

@@ -1150,21 +1156,21 @@ impl<'a, T: ?Sized> PinMut<'a, T> {
11501156
/// the data out of the mutable reference you receive when you call this
11511157
/// function.
11521158
#[unstable(feature = "pin", issue = "49150")]
1153-
pub unsafe fn get_mut(this: PinMut<'a, T>) -> &'a mut T {
1159+
pub unsafe fn get_mut_unchecked(this: PinMut<'a, T>) -> &'a mut T {
11541160
this.inner
11551161
}
11561162

11571163
/// Construct a new pin by mapping the interior value.
11581164
///
1159-
/// For example, if you wanted to get a `PinMut` of a field of something, you
1160-
/// could use this to get access to that field in one line of code.
1165+
/// For example, if you wanted to get a `PinMut` of a field of something,
1166+
/// you could use this to get access to that field in one line of code.
11611167
///
11621168
/// This function is unsafe. You must guarantee that the data you return
11631169
/// will not move so long as the argument value does not move (for example,
11641170
/// because it is one of the fields of that value), and also that you do
11651171
/// not move out of the argument you receive to the interior function.
11661172
#[unstable(feature = "pin", issue = "49150")]
1167-
pub unsafe fn map<U, F>(this: PinMut<'a, T>, f: F) -> PinMut<'a, U> where
1173+
pub unsafe fn map_unchecked<U, F>(this: PinMut<'a, T>, f: F) -> PinMut<'a, U> where
11681174
F: FnOnce(&mut T) -> &mut U
11691175
{
11701176
PinMut { inner: f(this.inner) }

src/libcore/option.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ impl<T> Option<T> {
275275
#[unstable(feature = "pin", issue = "49150")]
276276
pub fn as_pin_mut<'a>(self: PinMut<'a, Self>) -> Option<PinMut<'a, T>> {
277277
unsafe {
278-
PinMut::get_mut(self).as_mut().map(|x| PinMut::new_unchecked(x))
278+
PinMut::get_mut_unchecked(self).as_mut().map(|x| PinMut::new_unchecked(x))
279279
}
280280
}
281281

src/libstd/future.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<T: Generator<Yield = ()>> !Unpin for GenFuture<T> {}
4343
impl<T: Generator<Yield = ()>> Future for GenFuture<T> {
4444
type Output = T::Return;
4545
fn poll(self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
46-
set_task_cx(cx, || match unsafe { PinMut::get_mut(self).0.resume() } {
46+
set_task_cx(cx, || match unsafe { PinMut::get_mut_unchecked(self).0.resume() } {
4747
GeneratorState::Yielded(()) => Poll::Pending,
4848
GeneratorState::Complete(x) => Poll::Ready(x),
4949
})

src/libstd/panic.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,9 @@ impl<T: fmt::Debug> fmt::Debug for AssertUnwindSafe<T> {
327327
impl<'a, F: Future> Future for AssertUnwindSafe<F> {
328328
type Output = F::Output;
329329

330-
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
331-
unsafe {
332-
let pinned_field = PinMut::new_unchecked(
333-
&mut PinMut::get_mut(self.reborrow()).0
334-
);
335-
336-
pinned_field.poll(cx)
337-
}
330+
fn poll(self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
331+
let pinned_field = unsafe { PinMut::map_unchecked(self, |x| &mut x.0) };
332+
pinned_field.poll(cx)
338333
}
339334
}
340335

0 commit comments

Comments
 (0)