Skip to content

Commit 3c77043

Browse files
committed
Rollup merge of rust-lang#53874 - withoutboats:pin-ptr-impls, r=RalfJung
Implement Unpin for Box, Rc, and Arc Per the discussion in rust-lang#49150, these should implement `Unpin` even if what they point to does not.
2 parents 3792dbd + 972cd8b commit 3c77043

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/liballoc/boxed.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,31 @@ impl<T: ?Sized> AsMut<T> for Box<T> {
749749
}
750750
}
751751

752+
/* Nota bene
753+
*
754+
* We could have chosen not to add this impl, and instead have written a
755+
* function of Pin<Box<T>> to Pin<T>. Such a function would not be sound,
756+
* because Box<T> implements Unpin even when T does not, as a result of
757+
* this impl.
758+
*
759+
* We chose this API instead of the alternative for a few reasons:
760+
* - Logically, it is helpful to understand pinning in regard to the
761+
* memory region being pointed to. For this reason none of the
762+
* standard library pointer types support projecting through a pin
763+
* (Box<T> is the only pointer type in std for which this would be
764+
* safe.)
765+
* - It is in practice very useful to have Box<T> be unconditionally
766+
* Unpin because of trait objects, for which the structural auto
767+
* trait functionality does not apply (e.g. Box<dyn Foo> would
768+
* otherwise not be Unpin).
769+
*
770+
* Another type with the same semantics as Box but only a conditional
771+
* implementation of `Unpin` (where `T: Unpin`) would be valid/safe, and
772+
* could have a method to project a Pin<T> from it.
773+
*/
774+
#[unstable(feature = "pin", issue = "49150")]
775+
impl<T: ?Sized> Unpin for Box<T> { }
776+
752777
#[unstable(feature = "generator_trait", issue = "43122")]
753778
impl<T> Generator for Box<T>
754779
where T: Generator + ?Sized

src/liballoc/rc.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ use core::fmt;
252252
use core::hash::{Hash, Hasher};
253253
use core::intrinsics::abort;
254254
use core::marker;
255-
use core::marker::{Unsize, PhantomData};
255+
use core::marker::{Unpin, Unsize, PhantomData};
256256
use core::mem::{self, align_of_val, forget, size_of_val};
257257
use core::ops::Deref;
258258
use core::ops::CoerceUnsized;
@@ -1830,3 +1830,6 @@ impl<T: ?Sized> AsRef<T> for Rc<T> {
18301830
&**self
18311831
}
18321832
}
1833+
1834+
#[unstable(feature = "pin", issue = "49150")]
1835+
impl<T: ?Sized> Unpin for Rc<T> { }

src/liballoc/sync.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use core::mem::{self, align_of_val, size_of_val};
2727
use core::ops::Deref;
2828
use core::ops::CoerceUnsized;
2929
use core::ptr::{self, NonNull};
30-
use core::marker::{Unsize, PhantomData};
30+
use core::marker::{Unpin, Unsize, PhantomData};
3131
use core::hash::{Hash, Hasher};
3232
use core::{isize, usize};
3333
use core::convert::From;
@@ -1943,3 +1943,6 @@ impl<T: ?Sized> AsRef<T> for Arc<T> {
19431943
&**self
19441944
}
19451945
}
1946+
1947+
#[unstable(feature = "pin", issue = "49150")]
1948+
impl<T: ?Sized> Unpin for Arc<T> { }

0 commit comments

Comments
 (0)