From 8b0cd801becb3b3c35a0b6f1d9adaa7304fdc724 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Fri, 9 Aug 2024 20:19:38 -0700 Subject: [PATCH] [unstable-rust] Use `async_closure` syntax. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Note: This causes type inference failures, requiring us to specify the closures’ parameter types explicitly. I gather from reading issue that this may be rectified by using native `AsyncFnOnce` / `async FnOnce` syntax, but that currently cannot specify the `+ Send + 'static` bound without also using `return_type_notation`, which is an incomplete feature. --- all-is-cubes-desktop/src/lib.rs | 1 + all-is-cubes-desktop/src/session.rs | 37 +++++++++++++++-------------- all-is-cubes-desktop/src/startup.rs | 2 +- all-is-cubes-ui/src/apps/session.rs | 2 +- all-is-cubes-ui/src/lib.rs | 1 + test-renderers/tests/wgpu-render.rs | 4 +++- 6 files changed, 26 insertions(+), 21 deletions(-) diff --git a/all-is-cubes-desktop/src/lib.rs b/all-is-cubes-desktop/src/lib.rs index b67903c00..27ed10de9 100644 --- a/all-is-cubes-desktop/src/lib.rs +++ b/all-is-cubes-desktop/src/lib.rs @@ -1,3 +1,4 @@ +#![feature(async_closure)] #![feature(never_type)] //! Components for creating a desktop application that renders interactive [`all_is_cubes`] diff --git a/all-is-cubes-desktop/src/session.rs b/all-is-cubes-desktop/src/session.rs index 11f64e89e..c06c251cf 100644 --- a/all-is-cubes-desktop/src/session.rs +++ b/all-is-cubes-desktop/src/session.rs @@ -9,7 +9,7 @@ use all_is_cubes::universe::Universe; use all_is_cubes::universe::UniverseStepInfo; use all_is_cubes::util::ErrorChain; use all_is_cubes_render::camera::Viewport; -use all_is_cubes_ui::apps::ExitMainTask; +use all_is_cubes_ui::apps::{ExitMainTask, MainTaskContext}; use crate::Session; @@ -148,25 +148,26 @@ impl DesktopSession { // TODO: Also make a way to do this that isn't replacing the main task, // or that defines a way for the existing main task to coordinate. - self.session.set_main_task(move |mut ctx| async move { - // TODO: Offer confirmation before replacing the current universe. - // TODO: Then open a progress-bar UI page while we load. - - match loader_task.await.unwrap() { - Ok(universe) => { - ctx.set_universe(universe); - } - Err(e) => { - ctx.show_modal_message(arcstr::format!( - "Failed to load file '{path}':\n{e}", - path = path.display(), - e = ErrorChain(&e), - )); + self.session + .set_main_task(async move |mut ctx: MainTaskContext| { + // TODO: Offer confirmation before replacing the current universe. + // TODO: Then open a progress-bar UI page while we load. + + match loader_task.await.unwrap() { + Ok(universe) => { + ctx.set_universe(universe); + } + Err(e) => { + ctx.show_modal_message(arcstr::format!( + "Failed to load file '{path}':\n{e}", + path = path.display(), + e = ErrorChain(&e), + )); + } } - } - ExitMainTask - }) + ExitMainTask + }) } /// Set the “fixed” window title — the portion of the title not determined by the universe, diff --git a/all-is-cubes-desktop/src/startup.rs b/all-is-cubes-desktop/src/startup.rs index 0bf0af451..9da315c8d 100644 --- a/all-is-cubes-desktop/src/startup.rs +++ b/all-is-cubes-desktop/src/startup.rs @@ -88,7 +88,7 @@ pub fn inner_main( match *options {} } - dsession.session.set_main_task(|mut ctx| async move { + dsession.session.set_main_task(async move |mut ctx| { let universe_result: Result = match universe_task_future.await { // nested Results because one is template failure and the other is tokio JoinHandle failure Ok(Ok(u)) => Ok(u), diff --git a/all-is-cubes-ui/src/apps/session.rs b/all-is-cubes-ui/src/apps/session.rs index a7fcac7b5..95395af55 100644 --- a/all-is-cubes-ui/src/apps/session.rs +++ b/all-is-cubes-ui/src/apps/session.rs @@ -1370,7 +1370,7 @@ mod tests { let mut cameras = session.create_cameras(ListenableSource::constant(Viewport::ARBITRARY)); session.set_main_task({ let noticed_step = noticed_step.clone(); - move |mut ctx| async move { + async move |mut ctx: MainTaskContext| { eprintln!("main task: waiting for new universe"); let new_universe = recv.await.unwrap(); ctx.set_universe(new_universe); diff --git a/all-is-cubes-ui/src/lib.rs b/all-is-cubes-ui/src/lib.rs index 454f6926d..8ec9ef011 100644 --- a/all-is-cubes-ui/src/lib.rs +++ b/all-is-cubes-ui/src/lib.rs @@ -1,3 +1,4 @@ +#![feature(async_closure)] #![feature(never_type)] #![feature(noop_waker)] diff --git a/test-renderers/tests/wgpu-render.rs b/test-renderers/tests/wgpu-render.rs index 09a3286c4..0ccb6fbb1 100644 --- a/test-renderers/tests/wgpu-render.rs +++ b/test-renderers/tests/wgpu-render.rs @@ -1,5 +1,7 @@ //! Runs [`test_renderers::harness_main`] against [`all_is_cubes_gpu::in_wgpu`]. +#![feature(async_closure)] + use clap::Parser as _; use tokio::sync::OnceCell; @@ -29,7 +31,7 @@ async fn main() -> test_renderers::HarnessResult { RendererId::Wgpu, test_renderers::SuiteId::Renderers, test_renderers::test_cases::all_tests, - move |label| async move { get_factory(label).await.unwrap() }, + async move |label| get_factory(label).await.unwrap(), parallelism, ) .await