Remove executor::ForegroundTask wrapper

Nathan Sobo and Max Brunsfeld created

Now that smol returns the same Task as async_task, we can simply re-export the async_task::Task struct from our executor module.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>

Change summary

Cargo.lock           | 35 +++-----------------------------
Cargo.toml           |  3 ++
gpui/Cargo.toml      |  3 -
gpui/src/app.rs      | 14 ++++++------
gpui/src/executor.rs | 49 +++------------------------------------------
5 files changed, 19 insertions(+), 85 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -59,7 +59,7 @@ version = "1.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "eb877970c7b440ead138f6321a3b5395d6061183af779340b65e20c0fede9146"
 dependencies = [
- "async-task 4.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "async-task",
  "concurrent-queue",
  "fastrand",
  "futures-lite",
@@ -138,13 +138,7 @@ dependencies = [
 [[package]]
 name = "async-task"
 version = "4.0.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0"
-
-[[package]]
-name = "async-task"
-version = "4.0.3"
-source = "git+https://github.com/zedit-io/async-task?rev=341b57d6de98cdfd7b418567b8de2022ca993a6e#341b57d6de98cdfd7b418567b8de2022ca993a6e"
+source = "git+https://github.com/zed-industries/async-task?rev=341b57d6de98cdfd7b418567b8de2022ca993a6e#341b57d6de98cdfd7b418567b8de2022ca993a6e"
 
 [[package]]
 name = "atomic-waker"
@@ -228,7 +222,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "c5e170dbede1f740736619b776d7251cb1b9095c435c34d8ca9f57fcd2f335e9"
 dependencies = [
  "async-channel",
- "async-task 4.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "async-task",
  "atomic-waker",
  "fastrand",
  "futures-lite",
@@ -781,7 +775,7 @@ name = "gpui"
 version = "0.1.0"
 dependencies = [
  "anyhow",
- "async-task 4.0.3 (git+https://github.com/zedit-io/async-task?rev=341b57d6de98cdfd7b418567b8de2022ca993a6e)",
+ "async-task",
  "bindgen",
  "cc",
  "cocoa",
@@ -800,7 +794,6 @@ dependencies = [
  "parking_lot",
  "pathfinder_color",
  "pathfinder_geometry",
- "pin-project",
  "png",
  "rand 0.8.3",
  "replace_with",
@@ -1106,26 +1099,6 @@ version = "0.1.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
 
-[[package]]
-name = "pin-project"
-version = "1.0.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "96fa8ebb90271c4477f144354485b8068bd8f6b78b428b01ba892ca26caf0b63"
-dependencies = [
- "pin-project-internal",
-]
-
-[[package]]
-name = "pin-project-internal"
-version = "1.0.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "758669ae3558c6f74bd2a18b41f7ac0b5a195aea6639d6a9b5e5d1ad5ba24c0b"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn",
-]
-
 [[package]]
 name = "pin-project-lite"
 version = "0.2.4"

Cargo.toml 🔗

@@ -1,2 +1,5 @@
 [workspace]
 members = ["zed", "gpui"]
+
+[patch.crates-io]
+async-task = {git = "https://github.com/zed-industries/async-task", rev = "341b57d6de98cdfd7b418567b8de2022ca993a6e"}

gpui/Cargo.toml 🔗

@@ -5,7 +5,7 @@ name = "gpui"
 version = "0.1.0"
 
 [dependencies]
-async-task = {git = "https://github.com/zedit-io/async-task", rev = "341b57d6de98cdfd7b418567b8de2022ca993a6e"}
+async-task = "4.0.3"
 ctor = "0.1"
 etagere = "0.2"
 num_cpus = "1.13"
@@ -13,7 +13,6 @@ ordered-float = "2.1.1"
 parking_lot = "0.11.1"
 pathfinder_color = "0.5"
 pathfinder_geometry = "0.5"
-pin-project = "1.0.5"
 rand = "0.8.3"
 replace_with = "0.1.7"
 smallvec = "1.6.1"

gpui/src/app.rs 🔗

@@ -1,6 +1,6 @@
 use crate::{
     elements::ElementBox,
-    executor::{self, ForegroundTask},
+    executor::{self, Task},
     keymap::{self, Keystroke},
     platform::{self, App as _, WindowOptions},
     presenter::Presenter,
@@ -963,7 +963,7 @@ impl MutableAppContext {
         self.flush_effects();
     }
 
-    fn spawn<F, T>(&mut self, future: F) -> (usize, ForegroundTask<Option<T>>)
+    fn spawn<F, T>(&mut self, future: F) -> (usize, Task<Option<T>>)
     where
         F: 'static + Future,
         T: 'static,
@@ -979,7 +979,7 @@ impl MutableAppContext {
         (task_id, task)
     }
 
-    fn spawn_stream<F, T>(&mut self, mut stream: F) -> (usize, ForegroundTask<Option<T>>)
+    fn spawn_stream<F, T>(&mut self, mut stream: F) -> (usize, Task<Option<T>>)
     where
         F: 'static + Stream + Unpin,
         T: 'static,
@@ -1525,7 +1525,7 @@ impl<'a, T: Entity> ModelContext<'a, T> {
             });
     }
 
-    pub fn spawn<S, F, U>(&mut self, future: S, callback: F) -> ForegroundTask<Option<U>>
+    pub fn spawn<S, F, U>(&mut self, future: S, callback: F) -> Task<Option<U>>
     where
         S: 'static + Future,
         F: 'static + FnOnce(&mut T, S::Output, &mut ModelContext<T>) -> U,
@@ -1557,7 +1557,7 @@ impl<'a, T: Entity> ModelContext<'a, T> {
         stream: S,
         mut item_callback: F,
         done_callback: G,
-    ) -> ForegroundTask<Option<U>>
+    ) -> Task<Option<U>>
     where
         S: 'static + Stream + Unpin,
         F: 'static + FnMut(&mut T, S::Item, &mut ModelContext<T>),
@@ -1785,7 +1785,7 @@ impl<'a, T: View> ViewContext<'a, T> {
         self.halt_stream = true;
     }
 
-    pub fn spawn<S, F, U>(&mut self, future: S, callback: F) -> ForegroundTask<Option<U>>
+    pub fn spawn<S, F, U>(&mut self, future: S, callback: F) -> Task<Option<U>>
     where
         S: 'static + Future,
         F: 'static + FnOnce(&mut T, S::Output, &mut ViewContext<T>) -> U,
@@ -1818,7 +1818,7 @@ impl<'a, T: View> ViewContext<'a, T> {
         stream: S,
         mut item_callback: F,
         done_callback: G,
-    ) -> ForegroundTask<Option<U>>
+    ) -> Task<Option<U>>
     where
         S: 'static + Stream + Unpin,
         F: 'static + FnMut(&mut T, S::Item, &mut ViewContext<T>),

gpui/src/executor.rs 🔗

@@ -1,6 +1,6 @@
 use anyhow::{anyhow, Result};
 use async_task::Runnable;
-use pin_project::pin_project;
+pub use async_task::Task;
 use smol::prelude::*;
 use smol::{channel, Executor};
 use std::rc::Rc;
@@ -17,27 +17,6 @@ pub enum Foreground {
     Test(smol::LocalExecutor<'static>),
 }
 
-#[must_use]
-#[pin_project(project = ForegroundTaskProject)]
-pub enum ForegroundTask<T> {
-    Platform(#[pin] async_task::Task<T>),
-    Test(#[pin] smol::Task<T>),
-}
-
-impl<T> Future for ForegroundTask<T> {
-    type Output = T;
-
-    fn poll(
-        self: std::pin::Pin<&mut Self>,
-        ctx: &mut std::task::Context<'_>,
-    ) -> std::task::Poll<Self::Output> {
-        match self.project() {
-            ForegroundTaskProject::Platform(task) => task.poll(ctx),
-            ForegroundTaskProject::Test(task) => task.poll(ctx),
-        }
-    }
-}
-
 pub struct Background {
     executor: Arc<smol::Executor<'static>>,
     _stop: channel::Sender<()>,
@@ -62,19 +41,16 @@ impl Foreground {
         Self::Test(smol::LocalExecutor::new())
     }
 
-    pub fn spawn<T: 'static>(
-        &self,
-        future: impl Future<Output = T> + 'static,
-    ) -> ForegroundTask<T> {
+    pub fn spawn<T: 'static>(&self, future: impl Future<Output = T> + 'static) -> Task<T> {
         match self {
             Self::Platform { dispatcher, .. } => {
                 let dispatcher = dispatcher.clone();
                 let schedule = move |runnable: Runnable| dispatcher.run_on_main_thread(runnable);
                 let (runnable, task) = async_task::spawn_local(future, schedule);
                 runnable.schedule();
-                ForegroundTask::Platform(task)
+                task
             }
-            Self::Test(executor) => ForegroundTask::Test(executor.spawn(future)),
+            Self::Test(executor) => executor.spawn(future),
         }
     }
 
@@ -86,23 +62,6 @@ impl Foreground {
     }
 }
 
-#[must_use]
-impl<T> ForegroundTask<T> {
-    pub fn detach(self) {
-        match self {
-            Self::Platform(task) => task.detach(),
-            Self::Test(task) => task.detach(),
-        }
-    }
-
-    pub async fn cancel(self) -> Option<T> {
-        match self {
-            Self::Platform(task) => task.cancel().await,
-            Self::Test(task) => task.cancel().await,
-        }
-    }
-}
-
 impl Background {
     pub fn new() -> Self {
         let executor = Arc::new(Executor::new());