From a44abfdc29ee66ec1d51c2389405cbac479f35a7 Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Tue, 22 May 2018 17:09:49 -0700 Subject: [PATCH 1/4] Make `Unpin` safe to implement --- src/liballoc/boxed.rs | 2 +- src/libcore/marker.rs | 2 +- src/libcore/mem.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index a156734423551..a83ce7f379fbc 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -850,4 +850,4 @@ impl fmt::Pointer for PinBox { impl, U: ?Sized> CoerceUnsized> for PinBox {} #[unstable(feature = "pin", issue = "49150")] -unsafe impl Unpin for PinBox {} +impl Unpin for PinBox {} diff --git a/src/libcore/marker.rs b/src/libcore/marker.rs index 6c8ee0eda11e9..d4a87b13f04d2 100644 --- a/src/libcore/marker.rs +++ b/src/libcore/marker.rs @@ -605,7 +605,7 @@ unsafe impl<'a, T: ?Sized> Freeze for &'a mut T {} /// /// [`PinMut`]: ../mem/struct.PinMut.html #[unstable(feature = "pin", issue = "49150")] -pub unsafe auto trait Unpin {} +pub auto trait Unpin {} /// Implementations of `Copy` for primitive types. /// diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 20445def634ec..116e56f4ae9fb 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -1207,4 +1207,4 @@ impl<'a, T: ?Sized> fmt::Pointer for PinMut<'a, T> { impl<'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized> for PinMut<'a, T> {} #[unstable(feature = "pin", issue = "49150")] -unsafe impl<'a, T: ?Sized> Unpin for PinMut<'a, T> {} +impl<'a, T: ?Sized> Unpin for PinMut<'a, T> {} From 640f6f07494ece4ebe7e4d9680fff9a83ab4b0d2 Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Tue, 22 May 2018 17:10:14 -0700 Subject: [PATCH 2/4] Add Pinned type for opting out of Unpin on stable --- src/libcore/marker.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libcore/marker.rs b/src/libcore/marker.rs index d4a87b13f04d2..77db165bcbde3 100644 --- a/src/libcore/marker.rs +++ b/src/libcore/marker.rs @@ -607,6 +607,16 @@ unsafe impl<'a, T: ?Sized> Freeze for &'a mut T {} #[unstable(feature = "pin", issue = "49150")] pub auto trait Unpin {} +/// A type which does not implement `Unpin`. +/// +/// If a type contains a `Pinned`, it will not implement `Unpin` by default. +#[unstable(feature = "pin", issue = "49150")] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +pub struct Pinned; + +#[unstable(feature = "pin", issue = "49150")] +impl !Unpin for Pinned {} + /// Implementations of `Copy` for primitive types. /// /// Implementations that cannot be described in Rust From b7ccb248480918ae589756b585f4cdcae82a26b7 Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Tue, 22 May 2018 16:59:53 -0700 Subject: [PATCH 3/4] Add PinMut::set --- src/libcore/mem.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 116e56f4ae9fb..059c099d66b56 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -1164,6 +1164,14 @@ impl<'a, T: ?Sized> PinMut<'a, T> { { PinMut { inner: f(this.inner) } } + + /// Assign a new value to the memory behind the pinned reference. + #[unstable(feature = "pin", issue = "49150")] + pub fn set(this: PinMut<'a, T>, value: T) + where T: Sized, + { + *this.inner = value; + } } #[unstable(feature = "pin", issue = "49150")] From 15d2f965d83672dc8e7edc972ab256e587878332 Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Tue, 22 May 2018 17:07:51 -0700 Subject: [PATCH 4/4] Add Option::as_pin_mut --- src/libcore/lib.rs | 1 + src/libcore/option.rs | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index d37629ced11db..2121bc4438056 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -74,6 +74,7 @@ #![deny(missing_debug_implementations)] #![feature(allow_internal_unstable)] +#![feature(arbitrary_self_types)] #![feature(asm)] #![feature(associated_type_defaults)] #![feature(attr_literals)] diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 28f37f72d6f9d..1e615042a6d88 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -147,6 +147,7 @@ use iter::{FromIterator, FusedIterator, TrustedLen}; use {mem, ops}; +use mem::PinMut; // Note that this is not a lang item per se, but it has a hidden dependency on // `Iterator`, which is one. The compiler assumes that the `next` method of @@ -269,6 +270,15 @@ impl Option { } } + /// Converts from `Option` to `Option>` + #[inline] + #[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)) + } + } + ///////////////////////////////////////////////////////////////////////// // Getting to contained values /////////////////////////////////////////////////////////////////////////