From 3bcb85ee658e7a5362f5e381c337f07381f916dc Mon Sep 17 00:00:00 2001 From: Josef Reinhard Brandl Date: Sat, 23 Jun 2018 13:32:53 +0200 Subject: [PATCH] `PinMut`: Add safe `get_mut` and rename unsafe fns to `get_mut_unchecked` and `map_unchecked` --- src/libcore/mem.rs | 14 ++++++++++---- src/libcore/option.rs | 2 +- src/libstd/future.rs | 2 +- src/libstd/panic.rs | 11 +++-------- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 31635ffa53c83..08bd9289ab487 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -1119,6 +1119,12 @@ impl<'a, T: ?Sized + Unpin> PinMut<'a, T> { pub fn new(reference: &'a mut T) -> PinMut<'a, T> { PinMut { inner: reference } } + + /// Get a mutable reference to the data inside of this `PinMut`. + #[unstable(feature = "pin", issue = "49150")] + pub fn get_mut(this: PinMut<'a, T>) -> &'a mut T { + this.inner + } } @@ -1150,21 +1156,21 @@ impl<'a, T: ?Sized> PinMut<'a, T> { /// the data out of the mutable reference you receive when you call this /// function. #[unstable(feature = "pin", issue = "49150")] - pub unsafe fn get_mut(this: PinMut<'a, T>) -> &'a mut T { + pub unsafe fn get_mut_unchecked(this: PinMut<'a, T>) -> &'a mut T { this.inner } /// Construct a new pin by mapping the interior value. /// - /// For example, if you wanted to get a `PinMut` of a field of something, you - /// could use this to get access to that field in one line of code. + /// For example, if you wanted to get a `PinMut` of a field of something, + /// you could use this to get access to that field in one line of code. /// /// This function is unsafe. You must guarantee that the data you return /// will not move so long as the argument value does not move (for example, /// because it is one of the fields of that value), and also that you do /// not move out of the argument you receive to the interior function. #[unstable(feature = "pin", issue = "49150")] - pub unsafe fn map(this: PinMut<'a, T>, f: F) -> PinMut<'a, U> where + pub unsafe fn map_unchecked(this: PinMut<'a, T>, f: F) -> PinMut<'a, U> where F: FnOnce(&mut T) -> &mut U { PinMut { inner: f(this.inner) } diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 1e615042a6d88..70979f631cdbf 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -275,7 +275,7 @@ impl Option { #[unstable(feature = "pin", issue = "49150")] pub fn as_pin_mut<'a>(self: PinMut<'a, Self>) -> Option> { unsafe { - PinMut::get_mut(self).as_mut().map(|x| PinMut::new_unchecked(x)) + PinMut::get_mut_unchecked(self).as_mut().map(|x| PinMut::new_unchecked(x)) } } diff --git a/src/libstd/future.rs b/src/libstd/future.rs index 2da775fdc949e..c1cc36f3b419a 100644 --- a/src/libstd/future.rs +++ b/src/libstd/future.rs @@ -43,7 +43,7 @@ impl> !Unpin for GenFuture {} impl> Future for GenFuture { type Output = T::Return; fn poll(self: PinMut, cx: &mut task::Context) -> Poll { - set_task_cx(cx, || match unsafe { PinMut::get_mut(self).0.resume() } { + set_task_cx(cx, || match unsafe { PinMut::get_mut_unchecked(self).0.resume() } { GeneratorState::Yielded(()) => Poll::Pending, GeneratorState::Complete(x) => Poll::Ready(x), }) diff --git a/src/libstd/panic.rs b/src/libstd/panic.rs index 2c11c262488b2..451420ae88ad8 100644 --- a/src/libstd/panic.rs +++ b/src/libstd/panic.rs @@ -327,14 +327,9 @@ impl fmt::Debug for AssertUnwindSafe { impl<'a, F: Future> Future for AssertUnwindSafe { type Output = F::Output; - fn poll(mut self: PinMut, cx: &mut task::Context) -> Poll { - unsafe { - let pinned_field = PinMut::new_unchecked( - &mut PinMut::get_mut(self.reborrow()).0 - ); - - pinned_field.poll(cx) - } + fn poll(self: PinMut, cx: &mut task::Context) -> Poll { + let pinned_field = unsafe { PinMut::map_unchecked(self, |x| &mut x.0) }; + pinned_field.poll(cx) } }