Move "async move" a few characters to the left in cx.spawn() (#26758)

This is the core change:
https://github.com/zed-industries/zed/pull/26758/files#diff-044302c0d57147af17e68a0009fee3e8dcdfb4f32c27a915e70cfa80e987f765R1052

TODO:
- [x] Use AsyncFn instead of Fn() -> Future in GPUI spawn methods
- [x] Implement it in the whole app
- [x] Implement it in the debugger 
- [x] Glance at the RPC crate, and see if those box future methods can
be switched over. Answer: It can't directly, as you can't make an
AsyncFn* into a trait object. There's ways around that, but they're all
more complex than just keeping the code as is.
- [ ] Fix platform specific code

Release Notes:

- N/A
This commit is contained in:
Mikayla Maki 2025-03-18 19:09:02 -07:00 committed by GitHub
parent 7f2e3fb5bd
commit 1aefa5178b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
256 changed files with 3110 additions and 3200 deletions

View file

@ -55,18 +55,18 @@ fn main() {
api_key,
));
cx.spawn(|mut cx| async move {
cx.spawn(async move |cx| {
let semantic_index = SemanticDb::new(
PathBuf::from("/tmp/semantic-index-db.mdb"),
embedding_provider,
&mut cx,
cx,
);
let mut semantic_index = semantic_index.await.unwrap();
let project_path = Path::new(&args[1]);
let project = Project::example([project_path], &mut cx).await;
let project = Project::example([project_path], cx).await;
cx.update(|cx| {
let language_registry = project.read(cx).languages().clone();
@ -113,7 +113,7 @@ fn main() {
let worktree = search_result.worktree.read(cx);
let entry_abs_path = worktree.abs_path().join(search_result.path.clone());
let fs = project.read(cx).fs().clone();
cx.spawn(|_| async move { fs.load(&entry_abs_path).await.unwrap() })
cx.spawn(async move |_| fs.load(&entry_abs_path).await.unwrap())
})
.unwrap()
.await;

View file

@ -229,7 +229,7 @@ impl EmbeddingIndex {
let language_registry = self.language_registry.clone();
let fs = self.fs.clone();
let (chunked_files_tx, chunked_files_rx) = channel::bounded(2048);
let task = cx.spawn(|cx| async move {
let task = cx.spawn(async move |cx| {
cx.background_executor()
.scoped(|cx| {
for _ in 0..cx.num_cpus() {

View file

@ -91,12 +91,9 @@ impl ProjectIndex {
last_status: Status::Idle,
embedding_provider,
_subscription: cx.subscribe(&project, Self::handle_project_event),
_maintain_status: cx.spawn(|this, mut cx| async move {
_maintain_status: cx.spawn(async move |this, cx| {
while status_rx.recv().await.is_ok() {
if this
.update(&mut cx, |this, cx| this.update_status(cx))
.is_err()
{
if this.update(cx, |this, cx| this.update_status(cx)).is_err() {
break;
}
}
@ -163,10 +160,10 @@ impl ProjectIndex {
cx,
);
let load_worktree = cx.spawn(|this, mut cx| async move {
let load_worktree = cx.spawn(async move |this, cx| {
let result = match worktree_index.await {
Ok(worktree_index) => {
this.update(&mut cx, |this, _| {
this.update(cx, |this, _| {
this.worktree_indices.insert(
worktree_id,
WorktreeIndexHandle::Loaded {
@ -177,14 +174,14 @@ impl ProjectIndex {
Ok(worktree_index)
}
Err(error) => {
this.update(&mut cx, |this, _cx| {
this.update(cx, |this, _cx| {
this.worktree_indices.remove(&worktree_id)
})?;
Err(Arc::new(error))
}
};
this.update(&mut cx, |this, cx| this.update_status(cx))?;
this.update(cx, |this, cx| this.update_status(cx))?;
result
});
@ -239,7 +236,7 @@ impl ProjectIndex {
for worktree_index in self.worktree_indices.values() {
let worktree_index = worktree_index.clone();
let chunks_tx = chunks_tx.clone();
worktree_scan_tasks.push(cx.spawn(|cx| async move {
worktree_scan_tasks.push(cx.spawn(async move |cx| {
let index = match worktree_index {
WorktreeIndexHandle::Loading { index } => {
index.clone().await.map_err(|error| anyhow!(error))?
@ -248,7 +245,7 @@ impl ProjectIndex {
};
index
.read_with(&cx, |index, cx| {
.read_with(cx, |index, cx| {
let worktree_id = index.worktree().read(cx).id();
let db_connection = index.db_connection().clone();
let db = *index.embedding_index().db();
@ -275,7 +272,7 @@ impl ProjectIndex {
let project = self.project.clone();
let embedding_provider = self.embedding_provider.clone();
cx.spawn(|cx| async move {
cx.spawn(async move |cx| {
#[cfg(debug_assertions)]
let embedding_query_start = std::time::Instant::now();
log::info!("Searching for {queries:?}");
@ -336,7 +333,7 @@ impl ProjectIndex {
scan_task.log_err();
}
project.read_with(&cx, |project, cx| {
project.read_with(cx, |project, cx| {
let mut search_results = Vec::with_capacity(results_by_worker.len() * limit);
for worker_results in results_by_worker {
search_results.extend(worker_results.into_iter().filter_map(|result| {
@ -419,7 +416,7 @@ impl ProjectIndex {
for worktree_index in self.worktree_indices.values() {
let worktree_index = worktree_index.clone();
let summaries_tx: channel::Sender<(String, String)> = summaries_tx.clone();
worktree_scan_tasks.push(cx.spawn(|cx| async move {
worktree_scan_tasks.push(cx.spawn(async move |cx| {
let index = match worktree_index {
WorktreeIndexHandle::Loading { index } => {
index.clone().await.map_err(|error| anyhow!(error))?
@ -428,7 +425,7 @@ impl ProjectIndex {
};
index
.read_with(&cx, |index, cx| {
.read_with(cx, |index, cx| {
let db_connection = index.db_connection().clone();
let summary_index = index.summary_index();
let file_digest_db = summary_index.file_digest_db();
@ -474,7 +471,7 @@ impl ProjectIndex {
drop(summaries_tx);
let project = self.project.clone();
cx.spawn(|cx| async move {
cx.spawn(async move |cx| {
let mut results_by_worker = Vec::new();
for _ in 0..cx.background_executor().num_cpus() {
results_by_worker.push(Vec::<FileSummary>::new());
@ -496,7 +493,7 @@ impl ProjectIndex {
scan_task.log_err();
}
project.read_with(&cx, |_project, _cx| {
project.read_with(cx, |_project, _cx| {
results_by_worker.into_iter().flatten().collect()
})
})
@ -509,7 +506,7 @@ impl ProjectIndex {
futures::future::join_all(self.worktree_indices.values().map(|worktree_index| {
let worktree_index = worktree_index.clone();
cx.spawn(|cx| async move {
cx.spawn(async move |cx| {
let index = match worktree_index {
WorktreeIndexHandle::Loading { index } => {
index.clone().await.map_err(|error| anyhow!(error))?
@ -520,7 +517,7 @@ impl ProjectIndex {
cx.update(|cx| index.read(cx).worktree().read(cx).abs_path())?;
index
.read_with(&cx, |index, cx| {
.read_with(cx, |index, cx| {
cx.background_spawn(
index.summary_index().flush_backlog(worktree_abs_path, cx),
)

View file

@ -51,12 +51,12 @@ impl ProjectIndexDebugView {
fn update_rows(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let worktree_indices = self.index.read(cx).worktree_indices(cx);
cx.spawn_in(window, |this, mut cx| async move {
cx.spawn_in(window, async move |this, cx| {
let mut rows = Vec::new();
for index in worktree_indices {
let (root_path, worktree_id, worktree_paths) =
index.read_with(&cx, |index, cx| {
index.read_with(cx, |index, cx| {
let worktree = index.worktree().read(cx);
(
worktree.abs_path(),
@ -73,7 +73,7 @@ impl ProjectIndexDebugView {
);
}
this.update(&mut cx, |this, cx| {
this.update(cx, |this, cx| {
this.rows = rows;
cx.notify();
})
@ -96,7 +96,7 @@ impl ProjectIndexDebugView {
.embedding_index()
.chunks_for_path(file_path.clone(), cx);
cx.spawn_in(window, |this, mut cx| async move {
cx.spawn_in(window, async move |this, cx| {
let chunks = chunks.await?;
let content = fs.load(&root_path.join(&file_path)).await?;
let chunks = chunks
@ -114,7 +114,7 @@ impl ProjectIndexDebugView {
})
.collect::<Vec<_>>();
this.update(&mut cx, |this, cx| {
this.update(cx, |this, cx| {
let view = cx.entity().downgrade();
this.selected_path = Some(PathState {
path: file_path,

View file

@ -422,7 +422,7 @@ impl SummaryIndex {
) -> MightNeedSummaryFiles {
let fs = self.fs.clone();
let (rx, tx) = channel::bounded(2048);
let task = cx.spawn(|cx| async move {
let task = cx.spawn(async move |cx| {
cx.background_executor()
.scoped(|cx| {
for _ in 0..cx.num_cpus() {
@ -490,7 +490,7 @@ impl SummaryIndex {
cx: &App,
) -> SummarizeFiles {
let (summarized_tx, summarized_rx) = channel::bounded(512);
let task = cx.spawn(|cx| async move {
let task = cx.spawn(async move |cx| {
while let Ok(file) = unsummarized_files.recv().await {
log::debug!("Summarizing {:?}", file);
let summary = cx
@ -564,7 +564,7 @@ impl SummaryIndex {
};
let code_len = code.len();
cx.spawn(|cx| async move {
cx.spawn(async move |cx| {
let stream = model.stream_completion(request, &cx);
cx.background_spawn(async move {
let answer: String = stream

View file

@ -49,7 +49,7 @@ impl WorktreeIndex {
let worktree_abs_path = worktree.read(cx).abs_path();
let embedding_fs = Arc::clone(&fs);
let summary_fs = fs;
cx.spawn(|mut cx| async move {
cx.spawn(async move |cx| {
let entries_being_indexed = Arc::new(IndexingEntrySet::new(status_tx));
let (embedding_index, summary_index) = cx
.background_spawn({
@ -138,7 +138,9 @@ impl WorktreeIndex {
summary_index,
worktree,
entry_ids_being_indexed,
_index_entries: cx.spawn(|this, cx| Self::index_entries(this, updated_entries_rx, cx)),
_index_entries: cx.spawn(async move |this, cx| {
Self::index_entries(this, updated_entries_rx, cx).await
}),
_subscription,
}
}
@ -166,10 +168,10 @@ impl WorktreeIndex {
async fn index_entries(
this: WeakEntity<Self>,
updated_entries: channel::Receiver<UpdatedEntriesSet>,
mut cx: AsyncApp,
cx: &mut AsyncApp,
) -> Result<()> {
let is_auto_available = cx.update(|cx| cx.wait_for_flag::<AutoCommand>())?.await;
let index = this.update(&mut cx, |this, cx| {
let index = this.update(cx, |this, cx| {
futures::future::try_join(
this.embedding_index.index_entries_changed_on_disk(cx),
this.summary_index
@ -183,7 +185,7 @@ impl WorktreeIndex {
.update(|cx| cx.has_flag::<AutoCommand>())
.unwrap_or(false);
let index = this.update(&mut cx, |this, cx| {
let index = this.update(cx, |this, cx| {
futures::future::try_join(
this.embedding_index
.index_updated_entries(updated_entries.clone(), cx),