diff options
| author | Without Boats <boats@mozilla.com> | 2018-09-01 06:12:10 +0200 |
|---|---|---|
| committer | Without Boats <boats@mozilla.com> | 2018-09-01 06:57:58 +0200 |
| commit | 974bdc80fe3214159dc30e0bbb76694900e613c0 (patch) | |
| tree | a4ce3c775b9c740ffffb0d7609fe14520fe1ec94 /src/libcore/future | |
| parent | e6b35b0e1115f008796e8313574e4a4739b6d39d (diff) | |
Update to a new pinning API.
Diffstat (limited to 'src/libcore/future')
| -rw-r--r-- | src/libcore/future/future.rs | 19 | ||||
| -rw-r--r-- | src/libcore/future/future_obj.rs | 35 |
2 files changed, 40 insertions, 14 deletions
diff --git a/src/libcore/future/future.rs b/src/libcore/future/future.rs index 520b6ebbbaa..6cf1925000e 100644 --- a/src/libcore/future/future.rs +++ b/src/libcore/future/future.rs @@ -12,8 +12,9 @@ reason = "futures in libcore are unstable", issue = "50547")] -use pin::PinMut; use marker::Unpin; +use ops; +use pin::Pin; use task::{self, Poll}; /// A future represents an asychronous computation. @@ -92,21 +93,25 @@ pub trait Future { /// [`Poll::Pending`]: ../task/enum.Poll.html#variant.Pending /// [`Poll::Ready(val)`]: ../task/enum.Poll.html#variant.Ready /// [`cx.waker()`]: ../task/struct.Context.html#method.waker - fn poll(self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output>; + fn poll(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<Self::Output>; } impl<'a, F: ?Sized + Future + Unpin> Future for &'a mut F { type Output = F::Output; - fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> { - F::poll(PinMut::new(&mut **self), cx) + fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<Self::Output> { + F::poll(Pin::new(&mut **self), cx) } } -impl<'a, F: ?Sized + Future> Future for PinMut<'a, F> { +impl<P, F> Future for Pin<P> where + P: ops::DerefMut<Target = F> + Unpin, + F: Future + ?Sized, +{ type Output = F::Output; - fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> { - F::poll((*self).reborrow(), cx) + fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<Self::Output> { + let pin: Pin<&mut F> = Pin::as_mut(&mut *self); + F::poll(pin, cx) } } diff --git a/src/libcore/future/future_obj.rs b/src/libcore/future/future_obj.rs index 68fe461aeae..b335cac6c1d 100644 --- a/src/libcore/future/future_obj.rs +++ b/src/libcore/future/future_obj.rs @@ -15,7 +15,8 @@ use fmt; use future::Future; use marker::{PhantomData, Unpin}; -use pin::PinMut; +use ops; +use pin::Pin; use task::{Context, Poll}; /// A custom trait object for polling futures, roughly akin to @@ -78,9 +79,9 @@ impl<'a, T> Future for LocalFutureObj<'a, T> { type Output = T; #[inline] - fn poll(self: PinMut<Self>, cx: &mut Context) -> Poll<T> { + fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<T> { unsafe { - (self.poll_fn)(self.ptr, cx) + ((*self).poll_fn)((*self).ptr, cx) } } } @@ -128,9 +129,11 @@ impl<'a, T> Future for FutureObj<'a, T> { type Output = T; #[inline] - fn poll(self: PinMut<Self>, cx: &mut Context) -> Poll<T> { - let pinned_field = unsafe { PinMut::map_unchecked(self, |x| &mut x.0) }; - pinned_field.poll(cx) + fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<T> { + let pinned_field: Pin<&mut LocalFutureObj<'a, T>> = unsafe { + Pin::map_unchecked_mut(self, |x| &mut x.0) + }; + LocalFutureObj::poll(pinned_field, cx) } } @@ -175,7 +178,25 @@ unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for &'a mut F } unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T> { - PinMut::new_unchecked(&mut *(ptr as *mut F)).poll(cx) + let p: Pin<&mut F> = Pin::new_unchecked(&mut *(ptr as *mut F)); + F::poll(p, cx) + } + + unsafe fn drop(_ptr: *mut ()) {} +} + +#[unstable(feature = "futures_api", issue = "50547")] +unsafe impl<'a, T, P, F> UnsafeFutureObj<'a, T> for Pin<P> where + P: ops::DerefMut<Target = F> + 'a, + F: Future<Output = T> + 'a, +{ + fn into_raw(mut self) -> *mut () { + unsafe { Pin::get_mut_unchecked(Pin::as_mut(&mut self)) as *mut F as *mut () } + } + + unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T> { + let future: Pin<&mut F> = Pin::new_unchecked(&mut *(ptr as *mut F)); + F::poll(future, cx) } unsafe fn drop(_ptr: *mut ()) {} |
