]> git.lizzy.rs Git - rust.git/commitdiff
add a few blanket future impls to std
authortinaun <tinagma@gmail.com>
Fri, 8 Jun 2018 20:45:27 +0000 (16:45 -0400)
committertinaun <tinagma@gmail.com>
Fri, 8 Jun 2018 21:56:59 +0000 (17:56 -0400)
src/liballoc/boxed.rs
src/liballoc/lib.rs
src/libcore/future.rs
src/libcore/task.rs
src/libstd/lib.rs
src/libstd/panic.rs

index a64b94b6517590e0c351750e102f2e49f99b5dc9..896d9dee3ee45ca5ca01e057cc44141fa527e95e 100644 (file)
@@ -914,6 +914,45 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<PinBox<U>> for PinBox<T> {}
 #[unstable(feature = "pin", issue = "49150")]
 impl<T: ?Sized> Unpin for PinBox<T> {}
 
+#[unstable(feature = "futures_api", issue = "50547")]
+impl<'a, F: ?Sized + Future + Unpin> Future for Box<F> {
+    type Output = F::Output;
+
+    fn poll(mut self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output> {
+        PinMut::new(&mut **self).poll(cx)
+    }
+}
+
+#[unstable(feature = "futures_api", issue = "50547")]
+impl<'a, F: ?Sized + Future> Future for PinBox<F> {
+    type Output = F::Output;
+
+    fn poll(mut self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output> {
+        self.as_pin_mut().poll(cx)
+    }
+}
+
+#[unstable(feature = "futures_api", issue = "50547")]
+unsafe impl<F: Future<Output = ()> + Send + 'static> UnsafePoll for Box<F> {
+    fn into_raw(self) -> *mut () {
+        unsafe {
+            mem::transmute(self)
+        }
+    }
+
+    unsafe fn poll(task: *mut (), cx: &mut Context) -> Poll<()> {
+        let ptr: *mut F = mem::transmute(task);
+        let pin: PinMut<F> = PinMut::new_unchecked(&mut *ptr);
+        pin.poll(cx)
+    }
+
+    unsafe fn drop(task: *mut ()) {
+        let ptr: *mut F = mem::transmute(task);
+        let boxed = Box::from_raw(ptr);
+        drop(boxed)
+    }
+}
+
 #[unstable(feature = "futures_api", issue = "50547")]
 unsafe impl<F: Future<Output = ()> + Send + 'static> UnsafePoll for PinBox<F> {
     fn into_raw(self) -> *mut () {
index 242c7d2e70f85ea74487fd47822470015b692454..a1139189c9a4446859fd32ffc78781e354960709 100644 (file)
@@ -80,6 +80,7 @@
 #![cfg_attr(test, feature(rand, test))]
 #![feature(allocator_api)]
 #![feature(allow_internal_unstable)]
+#![feature(arbitrary_self_types)]
 #![feature(ascii_ctype)]
 #![feature(box_into_raw_non_null)]
 #![feature(box_patterns)]
index b4d087f8edb6df285bcbf74f8e923de9d404b090..a8c8f69411ea6d705e921356da21bb38186f1c7a 100644 (file)
@@ -15,6 +15,7 @@
 //! Asynchronous values.
 
 use mem::PinMut;
+use marker::Unpin;
 use task::{self, Poll};
 
 /// A future represents an asychronous computation.
@@ -91,3 +92,19 @@ pub trait Future {
     /// about the behavior of `poll` after a future has completed.
     fn poll(self: PinMut<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)
+    }
+}
+
+impl<'a, F: ?Sized + Future> Future for PinMut<'a, F> {
+    type Output = F::Output;
+
+    fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
+        F::poll((*self).reborrow(), cx)
+    }
+}
index e46a6d41d7acfc24d90fec0161e1ee2c07c19e13..ab1c1da57906a1a6b2b80fb0b8ee16f384facfd9 100644 (file)
@@ -32,6 +32,12 @@ pub enum Poll<T> {
     Pending,
 }
 
+impl<T> From<T> for Poll<T> {
+    fn from(t: T) -> Poll<T> {
+        Poll::Ready(t)
+    }
+}
+
 /// A `Waker` is a handle for waking up a task by notifying its executor that it
 /// is ready to be run.
 ///
index 7bbc99b83be5665f91a74c2d401220fec362183a..bb23fe5fa918e3f8506c625be1d0251e8754f971 100644 (file)
 #![feature(allow_internal_unsafe)]
 #![feature(allow_internal_unstable)]
 #![feature(align_offset)]
+#![feature(arbitrary_self_types)]
 #![feature(array_error_internals)]
 #![feature(ascii_ctype)]
 #![feature(asm)]
index 229034eb7790b029c30b087b733212441fb4c581..b70de73991ffe1fbb90271521ed7183ce9c408a0 100644 (file)
 use any::Any;
 use cell::UnsafeCell;
 use fmt;
+use future::Future;
+use mem::PinMut;
 use ops::{Deref, DerefMut};
 use panicking;
 use ptr::{Unique, NonNull};
 use rc::Rc;
 use sync::{Arc, Mutex, RwLock, atomic};
+use task::{self, Poll};
 use thread::Result;
 
 #[stable(feature = "panic_hooks", since = "1.10.0")]
@@ -315,6 +318,21 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
     }
 }
 
+#[unstable(feature = "futures_api", issue = "50547")]
+impl<'a, F: Future> Future for AssertUnwindSafe<F> {
+    type Output = F::Output;
+
+    fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
+        unsafe {
+            let pinned_field = PinMut::new_unchecked(
+                &mut PinMut::get_mut(self.reborrow()).0
+            ); 
+
+            pinned_field.poll(cx) 
+        }
+    }
+}
+
 /// Invokes a closure, capturing the cause of an unwinding panic if one occurs.
 ///
 /// This function will return `Ok` with the closure's result if the closure