Remove executor::ForegroundTask wrapper

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>
This commit is contained in:
Nathan Sobo
2021-04-02 11:02:09 -06:00
co-authored by Max Brunsfeld
parent d68a747754
commit 914cf37f37
5 changed files with 19 additions and 85 deletions
+7 -7
View File
@@ -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>),
+4 -45
View File
@@ -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());