Skip to content

Commit

Permalink
Avoid a global mutable static
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Nov 30, 2024
1 parent a66852a commit 50d7168
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -494,16 +495,8 @@ fn get_quad_context() -> &'static mut dyn miniquad::RenderingBackend {
unsafe { &mut *CONTEXT.as_mut().unwrap().quad_context }

Check warning on line 495 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

creating a mutable reference to mutable static is discouraged

Check warning on line 495 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

creating a mutable reference to mutable static is discouraged

Check warning on line 495 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

creating a mutable reference to mutable static is discouraged

Check warning on line 495 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

creating a mutable reference to mutable static is discouraged

Check warning on line 495 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

creating a mutable reference to mutable static is discouraged
}

static mut MAIN_FUTURE: Option<Pin<Box<dyn Future<Output = ()>>>> = None;

struct Stage {}

impl Drop for Stage {
fn drop(&mut self) {
unsafe {
MAIN_FUTURE.take();
}
}
struct Stage {
main_future: Option<Pin<Box<dyn Future<Output = ()>>>>,
}

impl EventHandler for Stage {
Expand Down Expand Up @@ -727,26 +720,25 @@ impl EventHandler for Stage {
}
}

let result = maybe_unwind(get_context().unwind, || {
if let Some(future) = unsafe { MAIN_FUTURE.as_mut() } {
let _z = telemetry::ZoneGuard::new("Event::draw user code");
let result = maybe_unwind(
get_context().unwind,
AssertUnwindSafe(|| {
if let Some(future) = &mut self.main_future {
let _z = telemetry::ZoneGuard::new("Event::draw user code");

if exec::resume(future).is_some() {
unsafe {
MAIN_FUTURE = None;
if exec::resume(future).is_some() {
self.main_future = None;
miniquad::window::quit();
return;
}
miniquad::window::quit();
return;
get_context().coroutines_context.update();
}
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 = Some(recovery_future);
}
}

Expand Down Expand Up @@ -882,9 +874,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,
Expand All @@ -893,7 +882,9 @@ impl Window {
);
unsafe { CONTEXT = Some(context) };

Box::new(Stage {})
Box::new(Stage {
main_future: Some(Box::pin(future)),
})
});
}
}

0 comments on commit 50d7168

Please sign in to comment.