diff --git a/src/lib.rs b/src/lib.rs index 5a563ef0..c9d5b131 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,7 @@ use miniquad::*; use std::collections::{HashMap, HashSet}; use std::future::Future; +use std::panic::AssertUnwindSafe; use std::pin::Pin; mod exec; @@ -494,16 +495,8 @@ fn get_quad_context() -> &'static mut dyn miniquad::RenderingBackend { unsafe { &mut *CONTEXT.as_mut().unwrap().quad_context } } -static mut MAIN_FUTURE: Option>>> = None; - -struct Stage {} - -impl Drop for Stage { - fn drop(&mut self) { - unsafe { - MAIN_FUTURE.take(); - } - } +struct Stage { + main_future: Pin>>, } impl EventHandler for Stage { @@ -727,26 +720,23 @@ impl EventHandler for Stage { } } - let result = maybe_unwind(get_context().unwind, || { - if let Some(future) = unsafe { MAIN_FUTURE.as_mut() } { + let result = maybe_unwind( + get_context().unwind, + AssertUnwindSafe(|| { let _z = telemetry::ZoneGuard::new("Event::draw user code"); - if exec::resume(future).is_some() { - unsafe { - MAIN_FUTURE = None; - } + if exec::resume(&mut self.main_future).is_some() { + self.main_future = Box::pin(async move {}); miniquad::window::quit(); return; } get_context().coroutines_context.update(); - } - }); + }), + ); if result == false { if let Some(recovery_future) = get_context().recovery_future.take() { - unsafe { - MAIN_FUTURE = Some(recovery_future); - } + self.main_future = recovery_future; } } @@ -882,9 +872,6 @@ impl Window { } = config.into(); miniquad::start(miniquad_conf, move || { thread_assert::set_thread_id(); - unsafe { - MAIN_FUTURE = Some(Box::pin(future)); - } let context = Context::new( update_on.unwrap_or_default(), default_filter_mode, @@ -893,7 +880,9 @@ impl Window { ); unsafe { CONTEXT = Some(context) }; - Box::new(Stage {}) + Box::new(Stage { + main_future: Box::pin(future), + }) }); } }