From c3bdd760472583f45dca1d0c9c16012f84d3fa15 Mon Sep 17 00:00:00 2001 From: Without Boats Date: Sat, 1 Sep 2018 01:54:59 +0200 Subject: [PATCH 1/4] Implement Unpin for Box, Rc, and Arc --- src/liballoc/boxed.rs | 3 +++ src/liballoc/rc.rs | 5 ++++- src/liballoc/sync.rs | 5 ++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index c25f3eb8f1750..f16a112b80119 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -749,6 +749,9 @@ impl AsMut for Box { } } +#[unstable(feature = "pin", issue = "49150")] +impl Unpin for Box { } + #[unstable(feature = "generator_trait", issue = "43122")] impl Generator for Box where T: Generator + ?Sized diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 4860daa11e20c..488b9d32cf0a5 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -252,7 +252,7 @@ use core::fmt; use core::hash::{Hash, Hasher}; use core::intrinsics::abort; use core::marker; -use core::marker::{Unsize, PhantomData}; +use core::marker::{Unpin, Unsize, PhantomData}; use core::mem::{self, align_of_val, forget, size_of_val}; use core::ops::Deref; use core::ops::CoerceUnsized; @@ -1830,3 +1830,6 @@ impl AsRef for Rc { &**self } } + +#[unstable(feature = "pin", issue = "49150")] +impl Unpin for Box { } diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 2cd7898f4c781..da94ef4a9c4f1 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -27,7 +27,7 @@ use core::mem::{self, align_of_val, size_of_val}; use core::ops::Deref; use core::ops::CoerceUnsized; use core::ptr::{self, NonNull}; -use core::marker::{Unsize, PhantomData}; +use core::marker::{Unpin, Unsize, PhantomData}; use core::hash::{Hash, Hasher}; use core::{isize, usize}; use core::convert::From; @@ -1942,3 +1942,6 @@ impl AsRef for Arc { &**self } } + +#[unstable(feature = "pin", issue = "49150")] +impl Unpin for Arc { } From 9ff29d6188bf18451876715dcf215ca73452133a Mon Sep 17 00:00:00 2001 From: Without Boats Date: Sat, 1 Sep 2018 05:04:46 +0200 Subject: [PATCH 2/4] Fix Rc impl. --- src/liballoc/rc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 488b9d32cf0a5..2d65aaecbd0cb 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -1832,4 +1832,4 @@ impl AsRef for Rc { } #[unstable(feature = "pin", issue = "49150")] -impl Unpin for Box { } +impl Unpin for Rc { } From c82af09bb0fb78379cf3b525d22f902b3139217f Mon Sep 17 00:00:00 2001 From: Without Boats Date: Wed, 5 Sep 2018 23:47:10 +0200 Subject: [PATCH 3/4] Add comment. --- src/liballoc/boxed.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index f16a112b80119..aeed139ebb760 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -749,6 +749,28 @@ impl AsMut for Box { } } +/* Nota bene + * + * We could have chosen not to add this impl, and instead have written a + * function of Pin> to Pin. Such a function would not be sound, + * because Box implements Unpin even when T does not, as a result of + * this impl. + * + * We chose this API instead of the alternative for a few reasons: + * - Logically, it is helpful to understand pinning in regard to the + * memory region being pointed to. For this reason none of the + * standard library pointer types support projecting through a pin + * (Box is the only pointer type in std for which this would be + * safe.) + * - It is in practive very useful to have Box be unconditionally + * Unpin because of trait objects, for which the structural auto + * trait functionality does not apply (e.g. Box would + * otherwise not be Unpin). + * + * Another type with the same semantics as Box but only a conditional + * implementation of `Unpin` (where `T: Unpin`) would be valid/safe, and + * could have a method to project a Pin from it. + */ #[unstable(feature = "pin", issue = "49150")] impl Unpin for Box { } From 972cd8bb69d4c856735adc8abcfceab96a62f716 Mon Sep 17 00:00:00 2001 From: Without Boats Date: Thu, 6 Sep 2018 21:31:06 +0200 Subject: [PATCH 4/4] Fix typos. --- src/liballoc/boxed.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index aeed139ebb760..29ba433bec7f5 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -751,7 +751,7 @@ impl AsMut for Box { /* Nota bene * - * We could have chosen not to add this impl, and instead have written a + * We could have chosen not to add this impl, and instead have written a * function of Pin> to Pin. Such a function would not be sound, * because Box implements Unpin even when T does not, as a result of * this impl. @@ -762,7 +762,7 @@ impl AsMut for Box { * standard library pointer types support projecting through a pin * (Box is the only pointer type in std for which this would be * safe.) - * - It is in practive very useful to have Box be unconditionally + * - It is in practice very useful to have Box be unconditionally * Unpin because of trait objects, for which the structural auto * trait functionality does not apply (e.g. Box would * otherwise not be Unpin).