]> git.lizzy.rs Git - rust.git/commitdiff
Implement `UnsafeFutureObj` for `&mut Future`
authorJosef Reinhard Brandl <mail@josefbrandl.de>
Mon, 2 Jul 2018 17:07:59 +0000 (19:07 +0200)
committerJosef Reinhard Brandl <mail@josefbrandl.de>
Mon, 2 Jul 2018 17:07:59 +0000 (19:07 +0200)
src/liballoc/boxed.rs
src/libcore/future/future_obj.rs
src/libcore/mem.rs

index 7f6d27088b77c5b0bf18904daeaed8413d13aff0..fabeaa1c144bfbe79552cf0fbcd0fdbcf8c27fbb 100644 (file)
@@ -933,7 +933,9 @@ fn poll(mut self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output> {
 }
 
 #[unstable(feature = "futures_api", issue = "50547")]
-unsafe impl<'a, T, F: Future<Output = T> + 'a> UnsafeFutureObj<'a, T> for PinBox<F> {
+unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for PinBox<F>
+    where F: Future<Output = T> + 'a
+{
     fn into_raw(self) -> *mut () {
         PinBox::into_raw(self) as *mut ()
     }
index 99f212725388343cfabc8e908b5609e0d2638440..98c504a3f7bef380a6ca28cb044bc3c1c0bbf546 100644 (file)
@@ -14,7 +14,7 @@
 
 use fmt;
 use future::Future;
-use marker::PhantomData;
+use marker::{PhantomData, Unpin};
 use mem::PinMut;
 use task::{Context, Poll};
 
@@ -163,3 +163,17 @@ pub unsafe trait UnsafeFutureObj<'a, T>: 'a {
     /// other calls to `drop` or `poll`.
     unsafe fn drop(ptr: *mut ());
 }
+
+unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for &'a mut F
+    where F: Future<Output = T> + Unpin + 'a
+{
+    fn into_raw(self) -> *mut () {
+        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 ()) {}
+}
index b83c2e21a1a44d2a5c8274e4f09ee914be635305..84173654655ebbe28f496e5ebbe11b040fec1835 100644 (file)
@@ -1231,7 +1231,9 @@ impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<PinMut<'a, U>> for PinM
 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> {
+unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for PinMut<'a, F>
+    where F: Future<Output = T> + 'a
+{
     fn into_raw(self) -> *mut () {
         unsafe { PinMut::get_mut_unchecked(self) as *mut F as *mut () }
     }