@@ -59,12 +59,14 @@ use core::any::Any;
59
59
use core:: borrow;
60
60
use core:: cmp:: Ordering ;
61
61
use core:: fmt;
62
+ use core:: future:: Future ;
62
63
use core:: hash:: { Hash , Hasher } ;
63
64
use core:: iter:: FusedIterator ;
64
65
use core:: marker:: { Unpin , Unsize } ;
65
66
use core:: mem:: { self , PinMut } ;
66
67
use core:: ops:: { CoerceUnsized , Deref , DerefMut , Generator , GeneratorState } ;
67
68
use core:: ptr:: { self , NonNull , Unique } ;
69
+ use core:: task:: { Context , Poll , UnsafePoll , TaskObj } ;
68
70
use core:: convert:: From ;
69
71
70
72
use raw_vec:: RawVec ;
@@ -755,6 +757,7 @@ impl<T> Generator for Box<T>
755
757
/// A pinned, heap allocated reference.
756
758
#[ unstable( feature = "pin" , issue = "49150" ) ]
757
759
#[ fundamental]
760
+ #[ repr( transparent) ]
758
761
pub struct PinBox < T : ?Sized > {
759
762
inner : Box < T > ,
760
763
}
@@ -771,14 +774,72 @@ impl<T> PinBox<T> {
771
774
#[ unstable( feature = "pin" , issue = "49150" ) ]
772
775
impl < T : ?Sized > PinBox < T > {
773
776
/// Get a pinned reference to the data in this PinBox.
777
+ #[ inline]
774
778
pub fn as_pin_mut < ' a > ( & ' a mut self ) -> PinMut < ' a , T > {
775
779
unsafe { PinMut :: new_unchecked ( & mut * self . inner ) }
776
780
}
777
781
782
+ /// Constructs a `PinBox` from a raw pointer.
783
+ ///
784
+ /// After calling this function, the raw pointer is owned by the
785
+ /// resulting `PinBox`. Specifically, the `PinBox` destructor will call
786
+ /// the destructor of `T` and free the allocated memory. Since the
787
+ /// way `PinBox` allocates and releases memory is unspecified, the
788
+ /// only valid pointer to pass to this function is the one taken
789
+ /// from another `PinBox` via the [`PinBox::into_raw`] function.
790
+ ///
791
+ /// This function is unsafe because improper use may lead to
792
+ /// memory problems. For example, a double-free may occur if the
793
+ /// function is called twice on the same raw pointer.
794
+ ///
795
+ /// [`PinBox::into_raw`]: struct.PinBox.html#method.into_raw
796
+ ///
797
+ /// # Examples
798
+ ///
799
+ /// ```
800
+ /// #![feature(pin)]
801
+ /// use std::boxed::PinBox;
802
+ /// let x = PinBox::new(5);
803
+ /// let ptr = PinBox::into_raw(x);
804
+ /// let x = unsafe { PinBox::from_raw(ptr) };
805
+ /// ```
806
+ #[ inline]
807
+ pub unsafe fn from_raw ( raw : * mut T ) -> Self {
808
+ PinBox { inner : Box :: from_raw ( raw) }
809
+ }
810
+
811
+ /// Consumes the `PinBox`, returning the wrapped raw pointer.
812
+ ///
813
+ /// After calling this function, the caller is responsible for the
814
+ /// memory previously managed by the `PinBox`. In particular, the
815
+ /// caller should properly destroy `T` and release the memory. The
816
+ /// proper way to do so is to convert the raw pointer back into a
817
+ /// `PinBox` with the [`PinBox::from_raw`] function.
818
+ ///
819
+ /// Note: this is an associated function, which means that you have
820
+ /// to call it as `PinBox::into_raw(b)` instead of `b.into_raw()`. This
821
+ /// is so that there is no conflict with a method on the inner type.
822
+ ///
823
+ /// [`PinBox::from_raw`]: struct.PinBox.html#method.from_raw
824
+ ///
825
+ /// # Examples
826
+ ///
827
+ /// ```
828
+ /// #![feature(pin)]
829
+ /// use std::boxed::PinBox;
830
+ /// let x = PinBox::new(5);
831
+ /// let ptr = PinBox::into_raw(x);
832
+ /// ```
833
+ #[ inline]
834
+ pub fn into_raw ( b : PinBox < T > ) -> * mut T {
835
+ Box :: into_raw ( b. inner )
836
+ }
837
+
778
838
/// Get a mutable reference to the data inside this PinBox.
779
839
///
780
840
/// This function is unsafe. Users must guarantee that the data is never
781
841
/// moved out of this reference.
842
+ #[ inline]
782
843
pub unsafe fn get_mut < ' a > ( this : & ' a mut PinBox < T > ) -> & ' a mut T {
783
844
& mut * this. inner
784
845
}
@@ -787,6 +848,7 @@ impl<T: ?Sized> PinBox<T> {
787
848
///
788
849
/// This function is unsafe. Users must guarantee that the data is never
789
850
/// moved out of the box.
851
+ #[ inline]
790
852
pub unsafe fn unpin ( this : PinBox < T > ) -> Box < T > {
791
853
this. inner
792
854
}
@@ -851,3 +913,34 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<PinBox<U>> for PinBox<T> {}
851
913
852
914
#[ unstable( feature = "pin" , issue = "49150" ) ]
853
915
impl < T : ?Sized > Unpin for PinBox < T > { }
916
+
917
+ #[ unstable( feature = "futures_api" , issue = "50547" ) ]
918
+ unsafe impl < F : Future < Output = ( ) > + Send + ' static > UnsafePoll for PinBox < F > {
919
+ fn into_raw ( self ) -> * mut ( ) {
920
+ PinBox :: into_raw ( self ) as * mut ( )
921
+ }
922
+
923
+ unsafe fn poll ( task : * mut ( ) , cx : & mut Context ) -> Poll < ( ) > {
924
+ let ptr = task as * mut F ;
925
+ let pin: PinMut < F > = PinMut :: new_unchecked ( & mut * ptr) ;
926
+ pin. poll ( cx)
927
+ }
928
+
929
+ unsafe fn drop ( task : * mut ( ) ) {
930
+ drop ( PinBox :: from_raw ( task as * mut F ) )
931
+ }
932
+ }
933
+
934
+ #[ unstable( feature = "futures_api" , issue = "50547" ) ]
935
+ impl < F : Future < Output = ( ) > + Send + ' static > From < PinBox < F > > for TaskObj {
936
+ fn from ( boxed : PinBox < F > ) -> Self {
937
+ TaskObj :: from_poll_task ( boxed)
938
+ }
939
+ }
940
+
941
+ #[ unstable( feature = "futures_api" , issue = "50547" ) ]
942
+ impl < F : Future < Output = ( ) > + Send + ' static > From < Box < F > > for TaskObj {
943
+ fn from ( boxed : Box < F > ) -> Self {
944
+ TaskObj :: from_poll_task ( PinBox :: from ( boxed) )
945
+ }
946
+ }
0 commit comments