]> git.lizzy.rs Git - rust.git/commitdiff
Improve core::task::TaskObj
authorJosef Reinhard Brandl <mail@josefbrandl.de>
Wed, 13 Jun 2018 07:09:57 +0000 (09:09 +0200)
committerJosef Reinhard Brandl <mail@josefbrandl.de>
Wed, 13 Jun 2018 07:32:59 +0000 (09:32 +0200)
src/liballoc/boxed.rs
src/libcore/task.rs

index c794fb8220a9ef351e77041c2a0c72d561de1e34..ea60c7775af59f6881a77a6c59824acfc2db6f5f 100644 (file)
@@ -66,7 +66,7 @@
 use core::mem::{self, PinMut};
 use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState};
 use core::ptr::{self, NonNull, Unique};
-use core::task::{Context, Poll, UnsafePoll, TaskObj};
+use core::task::{Context, Poll, UnsafeTask, TaskObj};
 use core::convert::From;
 
 use raw_vec::RawVec;
@@ -933,7 +933,7 @@ fn poll(mut self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output> {
 }
 
 #[unstable(feature = "futures_api", issue = "50547")]
-unsafe impl<F: Future<Output = ()> + Send + 'static> UnsafePoll for PinBox<F> {
+unsafe impl<F: Future<Output = ()> + Send + 'static> UnsafeTask for PinBox<F> {
     fn into_raw(self) -> *mut () {
         PinBox::into_raw(self) as *mut ()
     }
@@ -952,13 +952,13 @@ unsafe fn drop(task: *mut ()) {
 #[unstable(feature = "futures_api", issue = "50547")]
 impl<F: Future<Output = ()> + Send + 'static> From<PinBox<F>> for TaskObj {
     fn from(boxed: PinBox<F>) -> Self {
-        TaskObj::from_poll_task(boxed)
+        TaskObj::new(boxed)
     }
 }
 
 #[unstable(feature = "futures_api", issue = "50547")]
 impl<F: Future<Output = ()> + Send + 'static> From<Box<F>> for TaskObj {
     fn from(boxed: Box<F>) -> Self {
-        TaskObj::from_poll_task(PinBox::from(boxed))
+        TaskObj::new(PinBox::from(boxed))
     }
 }
index bef6d3677d0a37232e7b27310340d1c45caaae80..1a6018ffb65adcdb1d20ca5415b771e30a0cef99 100644 (file)
@@ -16,6 +16,8 @@
 
 use fmt;
 use ptr::NonNull;
+use future::Future;
+use mem::PinMut;
 
 /// Indicates whether a value is available or if the current task has been
 /// scheduled to receive a wakeup instead.
@@ -455,8 +457,8 @@ fn status(&self) -> Result<(), SpawnErrorKind> {
 /// `Box<Future<Output = ()> + Send>`.
 pub struct TaskObj {
     ptr: *mut (),
-    poll: unsafe fn(*mut (), &mut Context) -> Poll<()>,
-    drop: unsafe fn(*mut ()),
+    poll_fn: unsafe fn(*mut (), &mut Context) -> Poll<()>,
+    drop_fn: unsafe fn(*mut ()),
 }
 
 impl fmt::Debug for TaskObj {
@@ -467,7 +469,6 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 }
 
 unsafe impl Send for TaskObj {}
-unsafe impl Sync for TaskObj {}
 
 /// A custom implementation of a task trait object for `TaskObj`, providing
 /// a hand-rolled vtable.
@@ -478,7 +479,7 @@ unsafe impl Sync for TaskObj {}
 /// The implementor must guarantee that it is safe to call `poll` repeatedly (in
 /// a non-concurrent fashion) with the result of `into_raw` until `drop` is
 /// called.
-pub unsafe trait UnsafePoll: Send + 'static {
+pub unsafe trait UnsafeTask: Send + 'static {
     /// Convert a owned instance into a (conceptually owned) void pointer.
     fn into_raw(self) -> *mut ();
 
@@ -504,22 +505,22 @@ pub unsafe trait UnsafePoll: Send + 'static {
 impl TaskObj {
     /// Create a `TaskObj` from a custom trait object representation.
     #[inline]
-    pub fn from_poll_task<T: UnsafePoll>(t: T) -> TaskObj {
+    pub fn new<T: UnsafeTask>(t: T) -> TaskObj {
         TaskObj {
             ptr: t.into_raw(),
-            poll: T::poll,
-            drop: T::drop,
+            poll_fn: T::poll,
+            drop_fn: T::drop,
         }
     }
+}
+
+impl Future for TaskObj {
+    type Output = ();
 
-    /// Poll the task.
-    ///
-    /// The semantics here are identical to that for futures, but unlike
-    /// futures only an `&mut self` reference is needed here.
     #[inline]
-    pub fn poll_task(&mut self, cx: &mut Context) -> Poll<()> {
+    fn poll(self: PinMut<Self>, cx: &mut Context) -> Poll<()> {
         unsafe {
-            (self.poll)(self.ptr, cx)
+            (self.poll_fn)(self.ptr, cx)
         }
     }
 }
@@ -527,7 +528,7 @@ pub fn poll_task(&mut self, cx: &mut Context) -> Poll<()> {
 impl Drop for TaskObj {
     fn drop(&mut self) {
         unsafe {
-            (self.drop)(self.ptr)
+            (self.drop_fn)(self.ptr)
         }
     }
 }