Rename Task -> EntityTask (a BackgroundTask is just a Task)

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2021-04-05 17:03:42 -07:00
parent 46f8665e41
commit fda1394057
7 changed files with 28 additions and 30 deletions

View file

@ -967,7 +967,7 @@ impl MutableAppContext {
self.flush_effects(); self.flush_effects();
} }
fn spawn<F, T>(&mut self, future: F) -> Task<Option<T>> fn spawn<F, T>(&mut self, future: F) -> EntityTask<Option<T>>
where where
F: 'static + Future, F: 'static + Future,
T: 'static, T: 'static,
@ -983,7 +983,7 @@ impl MutableAppContext {
.map(|result| *result.downcast::<T>().unwrap()) .map(|result| *result.downcast::<T>().unwrap())
}) })
}; };
Task::new( EntityTask::new(
task_id, task_id,
task, task,
TaskHandlerMap::Future(self.future_handlers.clone()), TaskHandlerMap::Future(self.future_handlers.clone()),
@ -991,7 +991,7 @@ impl MutableAppContext {
) )
} }
fn spawn_stream<F, T>(&mut self, mut stream: F) -> Task<Option<T>> fn spawn_stream<F, T>(&mut self, mut stream: F) -> EntityTask<Option<T>>
where where
F: 'static + Stream + Unpin, F: 'static + Stream + Unpin,
T: 'static, T: 'static,
@ -1021,7 +1021,7 @@ impl MutableAppContext {
}) })
}; };
Task::new( EntityTask::new(
task_id, task_id,
task, task,
TaskHandlerMap::Stream(self.stream_handlers.clone()), TaskHandlerMap::Stream(self.stream_handlers.clone()),
@ -1562,7 +1562,7 @@ impl<'a, T: Entity> ModelContext<'a, T> {
}); });
} }
pub fn spawn<S, F, U>(&mut self, future: S, callback: F) -> Task<Option<U>> pub fn spawn<S, F, U>(&mut self, future: S, callback: F) -> EntityTask<Option<U>>
where where
S: 'static + Future, S: 'static + Future,
F: 'static + FnOnce(&mut T, S::Output, &mut ModelContext<T>) -> U, F: 'static + FnOnce(&mut T, S::Output, &mut ModelContext<T>) -> U,
@ -1594,7 +1594,7 @@ impl<'a, T: Entity> ModelContext<'a, T> {
stream: S, stream: S,
mut item_callback: F, mut item_callback: F,
done_callback: G, done_callback: G,
) -> Task<Option<U>> ) -> EntityTask<Option<U>>
where where
S: 'static + Stream + Unpin, S: 'static + Stream + Unpin,
F: 'static + FnMut(&mut T, S::Item, &mut ModelContext<T>), F: 'static + FnMut(&mut T, S::Item, &mut ModelContext<T>),
@ -1822,7 +1822,7 @@ impl<'a, T: View> ViewContext<'a, T> {
self.halt_stream = true; self.halt_stream = true;
} }
pub fn spawn<S, F, U>(&mut self, future: S, callback: F) -> Task<Option<U>> pub fn spawn<S, F, U>(&mut self, future: S, callback: F) -> EntityTask<Option<U>>
where where
S: 'static + Future, S: 'static + Future,
F: 'static + FnOnce(&mut T, S::Output, &mut ViewContext<T>) -> U, F: 'static + FnOnce(&mut T, S::Output, &mut ViewContext<T>) -> U,
@ -1855,7 +1855,7 @@ impl<'a, T: View> ViewContext<'a, T> {
stream: S, stream: S,
mut item_callback: F, mut item_callback: F,
done_callback: G, done_callback: G,
) -> Task<Option<U>> ) -> EntityTask<Option<U>>
where where
S: 'static + Stream + Unpin, S: 'static + Stream + Unpin,
F: 'static + FnMut(&mut T, S::Item, &mut ViewContext<T>), F: 'static + FnMut(&mut T, S::Item, &mut ViewContext<T>),
@ -2363,7 +2363,7 @@ enum StreamHandler {
} }
#[must_use] #[must_use]
pub struct Task<T> { pub struct EntityTask<T> {
id: usize, id: usize,
task: Option<executor::Task<T>>, task: Option<executor::Task<T>>,
handler_map: TaskHandlerMap, handler_map: TaskHandlerMap,
@ -2376,7 +2376,7 @@ enum TaskHandlerMap {
Stream(Rc<RefCell<HashMap<usize, StreamHandler>>>), Stream(Rc<RefCell<HashMap<usize, StreamHandler>>>),
} }
impl<T> Task<T> { impl<T> EntityTask<T> {
fn new( fn new(
id: usize, id: usize,
task: executor::Task<T>, task: executor::Task<T>,
@ -2402,7 +2402,7 @@ impl<T> Task<T> {
} }
} }
impl<T> Future for Task<T> { impl<T> Future for EntityTask<T> {
type Output = T; type Output = T;
fn poll( fn poll(
@ -2414,7 +2414,7 @@ impl<T> Future for Task<T> {
} }
} }
impl<T> Drop for Task<T> { impl<T> Drop for EntityTask<T> {
fn drop(self: &mut Self) { fn drop(self: &mut Self) {
match &self.handler_map { match &self.handler_map {
TaskHandlerMap::Detached => { TaskHandlerMap::Detached => {

View file

@ -22,9 +22,6 @@ pub struct Background {
_stop: channel::Sender<()>, _stop: channel::Sender<()>,
} }
#[must_use]
pub type BackgroundTask<T> = smol::Task<T>;
impl Foreground { impl Foreground {
pub fn platform(dispatcher: Arc<dyn platform::Dispatcher>) -> Result<Self> { pub fn platform(dispatcher: Arc<dyn platform::Dispatcher>) -> Result<Self> {
if dispatcher.is_main_thread() { if dispatcher.is_main_thread() {
@ -82,7 +79,7 @@ impl Background {
} }
} }
pub fn spawn<T>(&self, future: impl Send + Future<Output = T> + 'static) -> BackgroundTask<T> pub fn spawn<T>(&self, future: impl Send + Future<Output = T> + 'static) -> Task<T>
where where
T: 'static + Send, T: 'static + Send,
{ {

View file

@ -17,6 +17,7 @@ pub use text_layout::TextLayoutCache;
mod util; mod util;
pub use elements::{Element, ElementBox}; pub use elements::{Element, ElementBox};
pub mod executor; pub mod executor;
pub use executor::Task;
pub mod keymap; pub mod keymap;
pub mod platform; pub mod platform;
pub use pathfinder_color as color; pub use pathfinder_color as color;

View file

@ -14,7 +14,7 @@ use crate::{
worktree::FileHandle, worktree::FileHandle,
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use gpui::{executor::BackgroundTask, AppContext, Entity, ModelContext}; use gpui::{AppContext, Entity, ModelContext, Task};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use rand::prelude::*; use rand::prelude::*;
use std::{ use std::{
@ -241,7 +241,7 @@ impl Buffer {
} }
} }
pub fn save(&self, ctx: &mut ModelContext<Self>) -> Option<BackgroundTask<Result<()>>> { pub fn save(&self, ctx: &mut ModelContext<Self>) -> Option<Task<Result<()>>> {
if let Some(file) = &self.file { if let Some(file) = &self.file {
let snapshot = self.snapshot(); let snapshot = self.snapshot();
Some(file.save(snapshot, ctx.app())) Some(file.save(snapshot, ctx.app()))

View file

@ -5,9 +5,9 @@ use super::{
use crate::{settings::Settings, watch, workspace}; use crate::{settings::Settings, watch, workspace};
use anyhow::Result; use anyhow::Result;
use gpui::{ use gpui::{
executor::BackgroundTask, fonts::Properties as FontProperties, keymap::Binding, text_layout, fonts::Properties as FontProperties, keymap::Binding, text_layout, App, AppContext, Element,
App, AppContext, Element, ElementBox, Entity, FontCache, ModelHandle, MutableAppContext, View, ElementBox, Entity, FontCache, ModelHandle, MutableAppContext, Task, View, ViewContext,
ViewContext, WeakViewHandle, WeakViewHandle,
}; };
use gpui::{geometry::vector::Vector2F, TextLayoutCache}; use gpui::{geometry::vector::Vector2F, TextLayoutCache};
use parking_lot::Mutex; use parking_lot::Mutex;
@ -1180,7 +1180,7 @@ impl workspace::ItemView for BufferView {
Some(clone) Some(clone)
} }
fn save(&self, ctx: &mut MutableAppContext) -> Option<BackgroundTask<Result<()>>> { fn save(&self, ctx: &mut MutableAppContext) -> Option<Task<Result<()>>> {
self.buffer.update(ctx, |buffer, ctx| buffer.save(ctx)) self.buffer.update(ctx, |buffer, ctx| buffer.save(ctx))
} }
} }

View file

@ -1,8 +1,8 @@
use super::{pane, Pane, PaneGroup, SplitDirection, Workspace}; use super::{pane, Pane, PaneGroup, SplitDirection, Workspace};
use crate::{settings::Settings, watch}; use crate::{settings::Settings, watch};
use gpui::{ use gpui::{
color::rgbu, elements::*, executor::BackgroundTask, keymap::Binding, AnyViewHandle, App, color::rgbu, elements::*, keymap::Binding, AnyViewHandle, App, AppContext, Entity, ModelHandle,
AppContext, Entity, ModelHandle, MutableAppContext, View, ViewContext, ViewHandle, MutableAppContext, Task, View, ViewContext, ViewHandle,
}; };
use log::{error, info}; use log::{error, info};
use std::{collections::HashSet, path::PathBuf}; use std::{collections::HashSet, path::PathBuf};
@ -22,7 +22,7 @@ pub trait ItemView: View {
{ {
None None
} }
fn save(&self, _: &mut MutableAppContext) -> Option<BackgroundTask<anyhow::Result<()>>> { fn save(&self, _: &mut MutableAppContext) -> Option<Task<anyhow::Result<()>>> {
None None
} }
} }
@ -35,7 +35,7 @@ pub trait ItemViewHandle: Send + Sync {
fn set_parent_pane(&self, pane: &ViewHandle<Pane>, app: &mut MutableAppContext); fn set_parent_pane(&self, pane: &ViewHandle<Pane>, app: &mut MutableAppContext);
fn id(&self) -> usize; fn id(&self) -> usize;
fn to_any(&self) -> AnyViewHandle; fn to_any(&self) -> AnyViewHandle;
fn save(&self, ctx: &mut MutableAppContext) -> Option<BackgroundTask<anyhow::Result<()>>>; fn save(&self, ctx: &mut MutableAppContext) -> Option<Task<anyhow::Result<()>>>;
} }
impl<T: ItemView> ItemViewHandle for ViewHandle<T> { impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
@ -71,7 +71,7 @@ impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
}) })
} }
fn save(&self, ctx: &mut MutableAppContext) -> Option<BackgroundTask<anyhow::Result<()>>> { fn save(&self, ctx: &mut MutableAppContext) -> Option<Task<anyhow::Result<()>>> {
self.update(ctx, |item, ctx| item.save(ctx.app_mut())) self.update(ctx, |item, ctx| item.save(ctx.app_mut()))
} }

View file

@ -11,7 +11,7 @@ use crate::{
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use crossbeam_channel as channel; use crossbeam_channel as channel;
use easy_parallel::Parallel; use easy_parallel::Parallel;
use gpui::{executor::BackgroundTask, AppContext, Entity, ModelContext, ModelHandle}; use gpui::{AppContext, Entity, ModelContext, ModelHandle, Task};
use ignore::dir::{Ignore, IgnoreBuilder}; use ignore::dir::{Ignore, IgnoreBuilder};
use parking_lot::RwLock; use parking_lot::RwLock;
use smol::prelude::*; use smol::prelude::*;
@ -356,7 +356,7 @@ impl Worktree {
entry_id: usize, entry_id: usize,
content: Snapshot, content: Snapshot,
ctx: &AppContext, ctx: &AppContext,
) -> BackgroundTask<Result<()>> { ) -> Task<Result<()>> {
let path = self.abs_entry_path(entry_id); let path = self.abs_entry_path(entry_id);
ctx.background_executor().spawn(async move { ctx.background_executor().spawn(async move {
let buffer_size = content.text_summary().bytes.min(10 * 1024); let buffer_size = content.text_summary().bytes.min(10 * 1024);
@ -468,7 +468,7 @@ impl FileHandle {
self.worktree.as_ref(app).load_history(self.entry_id) self.worktree.as_ref(app).load_history(self.entry_id)
} }
pub fn save<'a>(&self, content: Snapshot, ctx: &AppContext) -> BackgroundTask<Result<()>> { pub fn save<'a>(&self, content: Snapshot, ctx: &AppContext) -> Task<Result<()>> {
let worktree = self.worktree.as_ref(ctx); let worktree = self.worktree.as_ref(ctx);
worktree.save(self.entry_id, content, ctx) worktree.save(self.entry_id, content, ctx)
} }