about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJosef Reinhard Brandl <mail@josefbrandl.de>2018-07-01 09:28:16 +0200
committerJosef Reinhard Brandl <mail@josefbrandl.de>2018-07-02 13:59:40 +0200
commit042928f0f591a63e0a2ed31c932015f4248d4fa7 (patch)
tree304e3ee83eb3acb74ff10ba31405798c807c5c36
parentd8bf2223672973f9d86f6c173793bcfce7890cd8 (diff)
downloadrust-042928f0f591a63e0a2ed31c932015f4248d4fa7.tar.gz
rust-042928f0f591a63e0a2ed31c932015f4248d4fa7.zip
`UnsafeFutureObj` impl for `PinMut`
-rw-r--r--src/liballoc/boxed.rs13
-rw-r--r--src/libcore/future/future_obj.rs6
-rw-r--r--src/libcore/mem.rs17
3 files changed, 26 insertions, 10 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs
index 5984a992afc..7f6d27088b7 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<Output = T> + 'a> UnsafeFutureObj<'a, T> for PinBox
         PinBox::into_raw(self) as *mut ()
     }
 
-    unsafe fn poll(task: *mut (), cx: &mut Context) -> Poll<T> {
-        let ptr = task as *mut F;
+    unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T> {
+        let ptr = ptr as *mut F;
         let pin: PinMut<F> = 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 c60b8b97d34..67bd3de98c1 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<T>;
+    unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T>;
 
     /// 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 08bd9289ab4..5bc55300a97 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>, U: ?Sized> CoerceUnsized<PinMut<'a, U>> 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<Output = T> + '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<T> {
+        PinMut::new_unchecked(&mut *(ptr as *mut F)).poll(cx)
+    }
+
+    unsafe fn drop(ptr: *mut ()) {
+        drop(PinMut::new_unchecked(&mut *(ptr as *mut F)));
+    }
+}