diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 5984a992afc6..7f6d27088b77 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -58,17 +58,16 @@ use core::any::Any; use core::borrow; use core::cmp::Ordering; +use core::convert::From; use core::fmt; -use core::future::Future; +use core::future::{Future, FutureObj, LocalFutureObj, UnsafeFutureObj}; use core::hash::{Hash, Hasher}; use core::iter::FusedIterator; use core::marker::{Unpin, Unsize}; use core::mem::{self, PinMut}; use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState}; use core::ptr::{self, NonNull, Unique}; -use core::future::{FutureObj, LocalFutureObj, UnsafeFutureObj}; use core::task::{Context, Poll}; -use core::convert::From; use raw_vec::RawVec; use str::from_boxed_utf8_unchecked; @@ -939,14 +938,14 @@ unsafe impl<'a, T, F: Future + 'a> UnsafeFutureObj<'a, T> for PinBox PinBox::into_raw(self) as *mut () } - unsafe fn poll(task: *mut (), cx: &mut Context) -> Poll { - let ptr = task as *mut F; + unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll { + let ptr = ptr as *mut F; let pin: PinMut = PinMut::new_unchecked(&mut *ptr); pin.poll(cx) } - unsafe fn drop(task: *mut ()) { - drop(PinBox::from_raw(task as *mut F)) + unsafe fn drop(ptr: *mut ()) { + drop(PinBox::from_raw(ptr as *mut F)) } } diff --git a/src/libcore/future/future_obj.rs b/src/libcore/future/future_obj.rs index c60b8b97d34e..67bd3de98c1a 100644 --- a/src/libcore/future/future_obj.rs +++ b/src/libcore/future/future_obj.rs @@ -126,7 +126,7 @@ impl<'a, T> Future for FutureObj<'a, T> { /// a non-concurrent fashion) with the result of `into_raw` until `drop` is /// called. pub unsafe trait UnsafeFutureObj<'a, T>: 'a { - /// Convert a owned instance into a (conceptually owned) void pointer. + /// Convert an owned instance into a (conceptually owned) void pointer. fn into_raw(self) -> *mut (); /// Poll the future represented by the given void pointer. @@ -136,7 +136,7 @@ pub unsafe trait UnsafeFutureObj<'a, T>: 'a { /// The trait implementor must guarantee that it is safe to repeatedly call /// `poll` with the result of `into_raw` until `drop` is called; such calls /// are not, however, allowed to race with each other or with calls to `drop`. - unsafe fn poll(future: *mut (), cx: &mut Context) -> Poll; + unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll; /// Drops the future represented by the given void pointer. /// @@ -145,5 +145,5 @@ pub unsafe trait UnsafeFutureObj<'a, T>: 'a { /// The trait implementor must guarantee that it is safe to call this /// function once per `into_raw` invocation; that call cannot race with /// other calls to `drop` or `poll`. - unsafe fn drop(future: *mut ()); + unsafe fn drop(ptr: *mut ()); } diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 08bd9289ab48..5bc55300a973 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -18,10 +18,12 @@ use clone; use cmp; use fmt; +use future::{Future, UnsafeFutureObj}; use hash; use intrinsics; use marker::{Copy, PhantomData, Sized, Unpin, Unsize}; use ptr; +use task::{Context, Poll}; use ops::{Deref, DerefMut, CoerceUnsized}; #[stable(feature = "rust1", since = "1.0.0")] @@ -1227,3 +1229,18 @@ impl<'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized> for PinM #[unstable(feature = "pin", issue = "49150")] impl<'a, T: ?Sized> Unpin for PinMut<'a, T> {} + +#[unstable(feature = "futures_api", issue = "50547")] +unsafe impl<'a, T, F: Future + 'a> UnsafeFutureObj<'a, T> for PinMut<'a, F> { + fn into_raw(self) -> *mut () { + unsafe { PinMut::get_mut_unchecked(self) as *mut F as *mut () } + } + + unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll { + PinMut::new_unchecked(&mut *(ptr as *mut F)).poll(cx) + } + + unsafe fn drop(ptr: *mut ()) { + drop(PinMut::new_unchecked(&mut *(ptr as *mut F))); + } +}