From 5f5dc7916eb92b40d4f75f0e9239ef36945253d4 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 | 3 ++- all-is-cubes-desktop/src/session.rs | 37 +++++++++++++++-------------- all-is-cubes-ui/src/apps/session.rs | 2 +- all-is-cubes-ui/src/lib.rs | 1 + test-renderers/tests/wgpu-render.rs | 4 +++- 5 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 5a6f1b95e..f18da077a 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`] @@ -101,7 +102,7 @@ pub fn inner_main( .context("failed to configure session for recording")?; } - dsession.session.set_main_task(|mut ctx| async move { + dsession.session.set_main_task(async move |mut ctx| { let universe_result: Result = match universe_future.await { Ok(Ok(u)) => Ok(u), Ok(Err(e)) => { diff --git a/all-is-cubes-desktop/src/session.rs b/all-is-cubes-desktop/src/session.rs index 7b9d40693..4bb9ee1f6 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; @@ -145,25 +145,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-ui/src/apps/session.rs b/all-is-cubes-ui/src/apps/session.rs index e64f7dd68..6f7ff61a6 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 ca3e45449..99f3f4583 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(min_exhaustive_patterns)] #![feature(never_type)] #![feature(noop_waker)] diff --git a/test-renderers/tests/wgpu-render.rs b/test-renderers/tests/wgpu-render.rs index 80e51b93a..d985a36ef 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 || async move { get_factory().await.unwrap() }, + async move || get_factory().await.unwrap(), parallelism, ) .await