@@ -65,13 +65,12 @@ use core::hash::{Hash, Hasher};
65
65
use core:: iter:: FusedIterator ;
66
66
use core:: marker:: { Unpin , Unsize } ;
67
67
use core:: mem;
68
- use core:: pin:: PinMut ;
68
+ use core:: pin:: Pin ;
69
69
use core:: ops:: { CoerceUnsized , Deref , DerefMut , Generator , GeneratorState } ;
70
70
use core:: ptr:: { self , NonNull , Unique } ;
71
71
use core:: task:: { Context , Poll , Spawn , SpawnErrorKind , SpawnObjError } ;
72
72
73
73
use raw_vec:: RawVec ;
74
- use pin:: PinBox ;
75
74
use str:: from_boxed_utf8_unchecked;
76
75
77
76
/// A pointer type for heap allocation.
@@ -97,6 +96,12 @@ impl<T> Box<T> {
97
96
pub fn new ( x : T ) -> Box < T > {
98
97
box x
99
98
}
99
+
100
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
101
+ #[ inline( always) ]
102
+ pub fn pinned ( x : T ) -> Pin < Box < T > > {
103
+ ( box x) . into ( )
104
+ }
100
105
}
101
106
102
107
impl < T : ?Sized > Box < T > {
@@ -427,6 +432,16 @@ impl<T> From<T> for Box<T> {
427
432
}
428
433
}
429
434
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
+
430
445
#[ stable( feature = "box_from_slice" , since = "1.17.0" ) ]
431
446
impl < ' a , T : Copy > From < & ' a [ T ] > for Box < [ T ] > {
432
447
fn from ( slice : & ' a [ T ] ) -> Box < [ T ] > {
@@ -789,8 +804,8 @@ impl<T> Generator for Box<T>
789
804
impl < F : ?Sized + Future + Unpin > Future for Box < F > {
790
805
type Output = F :: Output ;
791
806
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)
794
809
}
795
810
}
796
811
@@ -804,8 +819,8 @@ unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Box<F>
804
819
805
820
unsafe fn poll ( ptr : * mut ( ) , cx : & mut Context ) -> Poll < T > {
806
821
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)
809
824
}
810
825
811
826
unsafe fn drop ( ptr : * mut ( ) ) {
@@ -843,9 +858,16 @@ impl<'a, F: Future<Output = ()> + 'a> From<Box<F>> for LocalFutureObj<'a, ()> {
843
858
}
844
859
}
845
860
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)
850
872
}
851
873
}
0 commit comments