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

@ -12,6 +12,7 @@ use std::{
future::Future,
sync::Arc,
};
use util::Deferred;
use super::{App, AsyncWindowContext, Entity, KeystrokeEvent};
@ -199,14 +200,14 @@ impl<'a, T: 'static> Context<'a, T> {
/// The function is provided a weak handle to the entity owned by this context and a context that can be held across await points.
/// The returned task must be held or detached.
#[track_caller]
pub fn spawn<Fut, R>(&self, f: impl FnOnce(WeakEntity<T>, AsyncApp) -> Fut) -> Task<R>
pub fn spawn<AsyncFn, R>(&self, f: AsyncFn) -> Task<R>
where
T: 'static,
Fut: Future<Output = R> + 'static,
AsyncFn: AsyncFnOnce(WeakEntity<T>, &mut AsyncApp) -> R + 'static,
R: 'static,
{
let this = self.weak_entity();
self.app.spawn(|cx| f(this, cx))
self.app.spawn(async move |cx| f(this, cx).await)
}
/// Convenience method for accessing view state in an event callback.
@ -224,6 +225,18 @@ impl<'a, T: 'static> Context<'a, T> {
}
}
/// Run something using this entity and cx, when the returned struct is dropped
pub fn on_drop(
&self,
f: impl FnOnce(&mut T, &mut Context<T>) + 'static,
) -> Deferred<impl FnOnce()> {
let this = self.weak_entity();
let mut cx = self.to_async();
util::defer(move || {
this.update(&mut cx, f).ok();
})
}
/// Focus the given view in the given window. View type is required to implement Focusable.
pub fn focus_view<W: Focusable>(&mut self, view: &Entity<W>, window: &mut Window) {
window.focus(&view.focus_handle(self));
@ -600,17 +613,13 @@ impl<'a, T: 'static> Context<'a, T> {
/// It's also given an [`AsyncWindowContext`], which can be used to access the state of the view across await points.
/// The returned future will be polled on the main thread.
#[track_caller]
pub fn spawn_in<Fut, R>(
&self,
window: &Window,
f: impl FnOnce(WeakEntity<T>, AsyncWindowContext) -> Fut,
) -> Task<R>
pub fn spawn_in<AsyncFn, R>(&self, window: &Window, f: AsyncFn) -> Task<R>
where
R: 'static,
Fut: Future<Output = R> + 'static,
AsyncFn: AsyncFnOnce(WeakEntity<T>, &mut AsyncWindowContext) -> R + 'static,
{
let view = self.weak_entity();
window.spawn(self, |mut cx| f(view, cx))
window.spawn(self, async move |cx| f(view, cx).await)
}
/// Register a callback to be invoked when the given global state changes.