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
+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());