@@ -65,13 +65,12 @@ use core::hash::{Hash, Hasher};
6565use core:: iter:: FusedIterator ;
6666use core:: marker:: { Unpin , Unsize } ;
6767use core:: mem;
68- use core:: pin:: PinMut ;
68+ use core:: pin:: Pin ;
6969use core:: ops:: { CoerceUnsized , Deref , DerefMut , Generator , GeneratorState } ;
7070use core:: ptr:: { self , NonNull , Unique } ;
7171use core:: task:: { Context , Poll , Spawn , SpawnErrorKind , SpawnObjError } ;
7272
7373use raw_vec:: RawVec ;
74- use pin:: PinBox ;
7574use str:: from_boxed_utf8_unchecked;
7675
7776/// A pointer type for heap allocation.
@@ -97,6 +96,12 @@ impl<T> Box<T> {
9796 pub fn new ( x : T ) -> Box < T > {
9897 box x
9998 }
99+
100+ #[ unstable( feature = "pin" , issue = "49150" ) ]
101+ #[ inline( always) ]
102+ pub fn pinned ( x : T ) -> Pin < Box < T > > {
103+ ( box x) . into ( )
104+ }
100105}
101106
102107impl < T : ?Sized > Box < T > {
@@ -427,6 +432,16 @@ impl<T> From<T> for Box<T> {
427432 }
428433}
429434
435+ #[ unstable( feature = "pin" , issue = "49150" ) ]
436+ impl < T > From < Box < T > > for Pin < Box < T > > {
437+ fn from ( boxed : Box < T > ) -> Self {
438+ // It's not possible to move or replace the insides of a `Pin<Box<T>>`
439+ // when `T: !Unpin`, so it's safe to pin it directly without any
440+ // additional requirements.
441+ unsafe { Pin :: new_unchecked ( boxed) }
442+ }
443+ }
444+
430445#[ stable( feature = "box_from_slice" , since = "1.17.0" ) ]
431446impl < ' a , T : Copy > From < & ' a [ T ] > for Box < [ T ] > {
432447 fn from ( slice : & ' a [ T ] ) -> Box < [ T ] > {
@@ -789,8 +804,8 @@ impl<T> Generator for Box<T>
789804impl < F : ?Sized + Future + Unpin > Future for Box < F > {
790805 type Output = F :: Output ;
791806
792- fn poll ( mut self : PinMut < Self > , cx : & mut Context ) -> Poll < Self :: Output > {
793- PinMut :: new ( & mut * * self ) . poll ( cx)
807+ fn poll ( mut self : Pin < & mut Self > , cx : & mut Context ) -> Poll < Self :: Output > {
808+ F :: poll ( Pin :: new ( & mut * self ) , cx)
794809 }
795810}
796811
@@ -804,8 +819,8 @@ unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Box<F>
804819
805820 unsafe fn poll ( ptr : * mut ( ) , cx : & mut Context ) -> Poll < T > {
806821 let ptr = ptr as * mut F ;
807- let pin: PinMut < F > = PinMut :: new_unchecked ( & mut * ptr) ;
808- pin . poll ( cx)
822+ let pin: Pin < & mut F > = Pin :: new_unchecked ( & mut * ptr) ;
823+ F :: poll ( pin , cx)
809824 }
810825
811826 unsafe fn drop ( ptr : * mut ( ) ) {
@@ -843,9 +858,16 @@ impl<'a, F: Future<Output = ()> + 'a> From<Box<F>> for LocalFutureObj<'a, ()> {
843858 }
844859}
845860
846- #[ unstable( feature = "pin" , issue = "49150" ) ]
847- impl < T : Unpin + ?Sized > From < PinBox < T > > for Box < T > {
848- fn from ( pinned : PinBox < T > ) -> Box < T > {
849- unsafe { PinBox :: unpin ( pinned) }
861+ #[ unstable( feature = "futures_api" , issue = "50547" ) ]
862+ impl < ' a , F : Future < Output = ( ) > + Send + ' a > From < Pin < Box < F > > > for FutureObj < ' a , ( ) > {
863+ fn from ( boxed : Pin < Box < F > > ) -> Self {
864+ FutureObj :: new ( boxed)
865+ }
866+ }
867+
868+ #[ unstable( feature = "futures_api" , issue = "50547" ) ]
869+ impl < ' a , F : Future < Output = ( ) > + ' a > From < Pin < Box < F > > > for LocalFutureObj < ' a , ( ) > {
870+ fn from ( boxed : Pin < Box < F > > ) -> Self {
871+ LocalFutureObj :: new ( boxed)
850872 }
851873}
0 commit comments