@@ -59,12 +59,14 @@ use core::any::Any;
5959use core:: borrow;
6060use core:: cmp:: Ordering ;
6161use core:: fmt;
62+ use core:: future:: Future ;
6263use core:: hash:: { Hash , Hasher } ;
6364use core:: iter:: FusedIterator ;
6465use core:: marker:: { Unpin , Unsize } ;
6566use core:: mem:: { self , PinMut } ;
6667use core:: ops:: { CoerceUnsized , Deref , DerefMut , Generator , GeneratorState } ;
6768use core:: ptr:: { self , NonNull , Unique } ;
69+ use core:: task:: { Context , Poll , UnsafePoll , TaskObj } ;
6870use core:: convert:: From ;
6971
7072use raw_vec:: RawVec ;
@@ -755,6 +757,7 @@ impl<T> Generator for Box<T>
755757/// A pinned, heap allocated reference.
756758#[ unstable( feature = "pin" , issue = "49150" ) ]
757759#[ fundamental]
760+ #[ repr( transparent) ]
758761pub struct PinBox < T : ?Sized > {
759762 inner : Box < T > ,
760763}
@@ -771,14 +774,72 @@ impl<T> PinBox<T> {
771774#[ unstable( feature = "pin" , issue = "49150" ) ]
772775impl < T : ?Sized > PinBox < T > {
773776 /// Get a pinned reference to the data in this PinBox.
777+ #[ inline]
774778 pub fn as_pin_mut < ' a > ( & ' a mut self ) -> PinMut < ' a , T > {
775779 unsafe { PinMut :: new_unchecked ( & mut * self . inner ) }
776780 }
777781
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+
778838 /// Get a mutable reference to the data inside this PinBox.
779839 ///
780840 /// This function is unsafe. Users must guarantee that the data is never
781841 /// moved out of this reference.
842+ #[ inline]
782843 pub unsafe fn get_mut < ' a > ( this : & ' a mut PinBox < T > ) -> & ' a mut T {
783844 & mut * this. inner
784845 }
@@ -787,6 +848,7 @@ impl<T: ?Sized> PinBox<T> {
787848 ///
788849 /// This function is unsafe. Users must guarantee that the data is never
789850 /// moved out of the box.
851+ #[ inline]
790852 pub unsafe fn unpin ( this : PinBox < T > ) -> Box < T > {
791853 this. inner
792854 }
@@ -851,3 +913,34 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<PinBox<U>> for PinBox<T> {}
851913
852914#[ unstable( feature = "pin" , issue = "49150" ) ]
853915impl < 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