diff --git a/gpui/src/executor.rs b/gpui/src/executor.rs index 9c7681e19e2830babb03092d9b53b630322cd659..0eba748f33d5ffd4334803a3a7bbb2d82156c37b 100644 --- a/gpui/src/executor.rs +++ b/gpui/src/executor.rs @@ -77,10 +77,9 @@ impl Deterministic { } } - pub fn spawn_from_foreground(&self, future: F) -> Task + fn spawn_from_foreground(&self, future: Pin>>) -> Task where T: 'static, - F: Future + 'static, { let backtrace = Backtrace::new_unresolved(); let scheduled_once = AtomicBool::new(false); @@ -100,10 +99,9 @@ impl Deterministic { task } - pub fn spawn(&self, future: F) -> Task + fn spawn(&self, future: Pin>>) -> Task where T: 'static + Send, - F: 'static + Send + Future, { let backtrace = Backtrace::new_unresolved(); let state = self.state.clone(); @@ -119,13 +117,11 @@ impl Deterministic { task } - pub fn run(&self, future: F) -> T + fn run(&self, mut future: Pin>>) -> T where T: 'static, - F: Future + 'static, { let woken = Arc::new(AtomicBool::new(false)); - let mut future = Box::pin(future); loop { if let Some(result) = self.run_internal(woken.clone(), &mut future) { return result; @@ -142,15 +138,17 @@ impl Deterministic { fn run_until_parked(&self) { let woken = Arc::new(AtomicBool::new(false)); - let future = std::future::pending::<()>(); - smol::pin!(future); - self.run_internal(woken, future); + let mut future = std::future::pending::<()>().boxed_local(); + self.run_internal(woken, &mut future); } - pub fn run_internal(&self, woken: Arc, mut future: F) -> Option + fn run_internal( + &self, + woken: Arc, + future: &mut Pin>>, + ) -> Option where T: 'static, - F: Future + Unpin, { let unparker = self.parker.lock().unparker(); let waker = waker_fn(move || { @@ -396,6 +394,7 @@ impl Foreground { } pub fn spawn(&self, future: impl Future + 'static) -> Task { + let future = future.boxed_local(); match self { Self::Platform { dispatcher, .. } => { let dispatcher = dispatcher.clone(); @@ -410,6 +409,7 @@ impl Foreground { } pub fn run(&self, future: impl 'static + Future) -> T { + let future = future.boxed_local(); match self { Self::Platform { .. } => panic!("you can't call run on a platform foreground executor"), Self::Test(executor) => smol::block_on(executor.run(future)), @@ -500,23 +500,27 @@ impl Background { T: 'static + Send, F: Send + Future + 'static, { + let future = future.boxed(); match self { Self::Production { executor, .. } => executor.spawn(future), Self::Deterministic(executor) => executor.spawn(future), } } - pub fn block_with_timeout(&self, timeout: Duration, mut future: F) -> Result + pub fn block_with_timeout( + &self, + timeout: Duration, + future: F, + ) -> Result>>> where T: 'static, F: 'static + Unpin + Future, { + let mut future = future.boxed_local(); if !timeout.is_zero() { let output = match self { - Self::Production { .. } => { - smol::block_on(util::timeout(timeout, Pin::new(&mut future))).ok() - } - Self::Deterministic(executor) => executor.block_on(Pin::new(&mut future)), + Self::Production { .. } => smol::block_on(util::timeout(timeout, &mut future)).ok(), + Self::Deterministic(executor) => executor.block_on(&mut future), }; if let Some(output) = output { return Ok(output);