diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 6a05ef68088..c1778ef101a 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -66,7 +66,7 @@ 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::task::{Context, Poll, UnsafeTask, TaskObj, LocalTaskObj}; +use core::task::{Context, Poll, UnsafeFutureObj, FutureObj, LocalFutureObj}; use core::convert::From; use raw_vec::RawVec; @@ -933,12 +933,12 @@ impl<'a, F: ?Sized + Future> Future for PinBox { } #[unstable(feature = "futures_api", issue = "50547")] -unsafe impl + 'static> UnsafeTask for PinBox { +unsafe impl + 'static> UnsafeFutureObj for PinBox { fn into_raw(self) -> *mut () { PinBox::into_raw(self) as *mut () } - unsafe fn poll(task: *mut (), cx: &mut Context) -> Poll<()> { + unsafe fn poll(task: *mut (), cx: &mut Context) -> Poll { let ptr = task as *mut F; let pin: PinMut = PinMut::new_unchecked(&mut *ptr); pin.poll(cx) @@ -950,29 +950,29 @@ unsafe impl + 'static> UnsafeTask for PinBox { } #[unstable(feature = "futures_api", issue = "50547")] -impl + Send + 'static> From> for TaskObj { - fn from(boxed: PinBox) -> Self { - TaskObj::new(boxed) +impl + Send + 'static> Into> for PinBox { + fn into(self) -> FutureObj { + FutureObj::new(self) } } #[unstable(feature = "futures_api", issue = "50547")] -impl + Send + 'static> From> for TaskObj { - fn from(boxed: Box) -> Self { - TaskObj::new(PinBox::from(boxed)) +impl + Send + 'static> Into> for Box { + fn into(self) -> FutureObj { + FutureObj::new(PinBox::from(self)) } } #[unstable(feature = "futures_api", issue = "50547")] -impl + 'static> From> for LocalTaskObj { - fn from(boxed: PinBox) -> Self { - LocalTaskObj::new(boxed) +impl + 'static> Into> for PinBox { + fn into(self) -> LocalFutureObj { + LocalFutureObj::new(self) } } #[unstable(feature = "futures_api", issue = "50547")] -impl + 'static> From> for LocalTaskObj { - fn from(boxed: Box) -> Self { - LocalTaskObj::new(PinBox::from(boxed)) +impl + 'static> Into> for Box { + fn into(self) -> LocalFutureObj { + LocalFutureObj::new(PinBox::from(self)) } } diff --git a/src/libcore/task/executor.rs b/src/libcore/task/executor.rs index 73bf80d2f99..55ea5e724c1 100644 --- a/src/libcore/task/executor.rs +++ b/src/libcore/task/executor.rs @@ -13,7 +13,7 @@ issue = "50547")] use fmt; -use super::{TaskObj, LocalTaskObj}; +use super::{FutureObj, LocalFutureObj}; /// A task executor. /// @@ -29,7 +29,7 @@ pub trait Executor { /// /// The executor may be unable to spawn tasks, either because it has /// been shut down or is resource-constrained. - fn spawn_obj(&mut self, task: TaskObj) -> Result<(), SpawnObjError>; + fn spawn_obj(&mut self, task: FutureObj<()>) -> Result<(), SpawnObjError>; /// Determine whether the executor is able to spawn new tasks. /// @@ -76,7 +76,7 @@ pub struct SpawnObjError { pub kind: SpawnErrorKind, /// The task for which spawning was attempted - pub task: TaskObj, + pub task: FutureObj<()>, } /// The result of a failed spawn @@ -86,5 +86,5 @@ pub struct SpawnLocalObjError { pub kind: SpawnErrorKind, /// The task for which spawning was attempted - pub task: LocalTaskObj, + pub task: LocalFutureObj<()>, } diff --git a/src/libcore/task/task.rs b/src/libcore/task/future_obj.rs similarity index 50% rename from src/libcore/task/task.rs rename to src/libcore/task/future_obj.rs index c5a41873db4..3ed3bd51cf6 100644 --- a/src/libcore/task/task.rs +++ b/src/libcore/task/future_obj.rs @@ -14,63 +14,68 @@ use fmt; use future::Future; +use marker::PhantomData; use mem::PinMut; -use super::{Context, Poll}; +use task::{Context, Poll}; -/// A custom trait object for polling tasks, roughly akin to -/// `Box>`. -/// Contrary to `TaskObj`, `LocalTaskObj` does not have a `Send` bound. -pub struct LocalTaskObj { +/// A custom trait object for polling futures, roughly akin to +/// `Box>`. +/// Contrary to `FutureObj`, `LocalFutureObj` does not have a `Send` bound. +pub struct LocalFutureObj { ptr: *mut (), - poll_fn: unsafe fn(*mut (), &mut Context) -> Poll<()>, + poll_fn: unsafe fn(*mut (), &mut Context) -> Poll, drop_fn: unsafe fn(*mut ()), + _marker: PhantomData, } -impl LocalTaskObj { - /// Create a `LocalTaskObj` from a custom trait object representation. +impl LocalFutureObj { + /// Create a `LocalFutureObj` from a custom trait object representation. #[inline] - pub fn new(t: T) -> LocalTaskObj { - LocalTaskObj { - ptr: t.into_raw(), - poll_fn: T::poll, - drop_fn: T::drop, + pub fn new>(f: F) -> LocalFutureObj { + LocalFutureObj { + ptr: f.into_raw(), + poll_fn: F::poll, + drop_fn: F::drop, + _marker: PhantomData, } } - /// Converts the `LocalTaskObj` into a `TaskObj` - /// To make this operation safe one has to ensure that the `UnsafeTask` - /// instance from which this `LocalTaskObj` was created actually implements - /// `Send`. - pub unsafe fn as_task_obj(self) -> TaskObj { - TaskObj(self) + /// Converts the `LocalFutureObj` into a `FutureObj` + /// To make this operation safe one has to ensure that the `UnsafeFutureObj` + /// instance from which this `LocalFutureObj` was created actually + /// implements `Send`. + #[inline] + pub unsafe fn as_future_obj(self) -> FutureObj { + FutureObj(self) } } -impl fmt::Debug for LocalTaskObj { +impl fmt::Debug for LocalFutureObj { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("LocalTaskObj") + f.debug_struct("LocalFutureObj") .finish() } } -impl From for LocalTaskObj { - fn from(task: TaskObj) -> LocalTaskObj { - task.0 +impl From> for LocalFutureObj { + #[inline] + fn from(f: FutureObj) -> LocalFutureObj { + f.0 } } -impl Future for LocalTaskObj { - type Output = (); +impl Future for LocalFutureObj { + type Output = T; #[inline] - fn poll(self: PinMut, cx: &mut Context) -> Poll<()> { + fn poll(self: PinMut, cx: &mut Context) -> Poll { unsafe { (self.poll_fn)(self.ptr, cx) } } } -impl Drop for LocalTaskObj { +impl Drop for LocalFutureObj { fn drop(&mut self) { unsafe { (self.drop_fn)(self.ptr) @@ -78,38 +83,38 @@ impl Drop for LocalTaskObj { } } -/// A custom trait object for polling tasks, roughly akin to -/// `Box + Send>`. -pub struct TaskObj(LocalTaskObj); +/// A custom trait object for polling futures, roughly akin to +/// `Box> + Send`. +pub struct FutureObj(LocalFutureObj); -unsafe impl Send for TaskObj {} +unsafe impl Send for FutureObj {} -impl TaskObj { - /// Create a `TaskObj` from a custom trait object representation. +impl FutureObj { + /// Create a `FutureObj` from a custom trait object representation. #[inline] - pub fn new(t: T) -> TaskObj { - TaskObj(LocalTaskObj::new(t)) + pub fn new + Send>(f: F) -> FutureObj { + FutureObj(LocalFutureObj::new(f)) } } -impl fmt::Debug for TaskObj { +impl fmt::Debug for FutureObj { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("TaskObj") + f.debug_struct("FutureObj") .finish() } } -impl Future for TaskObj { - type Output = (); +impl Future for FutureObj { + type Output = T; #[inline] - fn poll(self: PinMut, cx: &mut Context) -> Poll<()> { + fn poll(self: PinMut, cx: &mut Context) -> Poll { let pinned_field = unsafe { PinMut::map_unchecked(self, |x| &mut x.0) }; pinned_field.poll(cx) } } -/// A custom implementation of a task trait object for `TaskObj`, providing +/// A custom implementation of a future trait object for `FutureObj`, providing /// a hand-rolled vtable. /// /// This custom representation is typically used only in `no_std` contexts, @@ -118,25 +123,25 @@ impl Future 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 UnsafeTask: 'static { +pub unsafe trait UnsafeFutureObj: 'static { /// Convert a owned instance into a (conceptually owned) void pointer. fn into_raw(self) -> *mut (); - /// Poll the task represented by the given void pointer. + /// Poll the future represented by the given void pointer. /// /// # Safety /// /// 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(task: *mut (), cx: &mut Context) -> Poll<()>; + unsafe fn poll(future: *mut (), cx: &mut Context) -> Poll; - /// Drops the task represented by the given void pointer. + /// Drops the future represented by the given void pointer. /// /// # Safety /// /// 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(task: *mut ()); + unsafe fn drop(future: *mut ()); } diff --git a/src/libcore/task/mod.rs b/src/libcore/task/mod.rs index d167a374105..06cd7a9dd77 100644 --- a/src/libcore/task/mod.rs +++ b/src/libcore/task/mod.rs @@ -25,8 +25,8 @@ pub use self::executor::{ mod poll; pub use self::poll::Poll; -mod task; -pub use self::task::{TaskObj, LocalTaskObj, UnsafeTask}; +mod future_obj; +pub use self::future_obj::{FutureObj, LocalFutureObj, UnsafeFutureObj}; mod wake; pub use self::wake::{Waker, LocalWaker, UnsafeWake}; diff --git a/src/test/run-pass/async-await.rs b/src/test/run-pass/async-await.rs index 8b649f6ef7b..3a67750e77e 100644 --- a/src/test/run-pass/async-await.rs +++ b/src/test/run-pass/async-await.rs @@ -21,7 +21,7 @@ use std::sync::{ }; use std::task::{ Context, Poll, Wake, - Executor, TaskObj, SpawnObjError, + Executor, FutureObj, SpawnObjError, local_waker_from_nonlocal, }; @@ -37,7 +37,7 @@ impl Wake for Counter { struct NoopExecutor; impl Executor for NoopExecutor { - fn spawn_obj(&mut self, _: TaskObj) -> Result<(), SpawnObjError> { + fn spawn_obj(&mut self, _: FutureObj) -> Result<(), SpawnObjError> { Ok(()) } } diff --git a/src/test/run-pass/futures-api.rs b/src/test/run-pass/futures-api.rs index 3b5a1725b66..a427b82af6a 100644 --- a/src/test/run-pass/futures-api.rs +++ b/src/test/run-pass/futures-api.rs @@ -22,7 +22,7 @@ use std::sync::{ use std::task::{ Context, Poll, Wake, Waker, LocalWaker, - Executor, TaskObj, SpawnObjError, + Executor, FutureObj, SpawnObjError, local_waker, local_waker_from_nonlocal, }; @@ -44,7 +44,7 @@ impl Wake for Counter { struct NoopExecutor; impl Executor for NoopExecutor { - fn spawn_obj(&mut self, _: TaskObj) -> Result<(), SpawnObjError> { + fn spawn_obj(&mut self, _: FutureObj<()>) -> Result<(), SpawnObjError> { Ok(()) } }