Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid a global mutable static #863

Merged
merged 2 commits into from
Nov 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 14 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

use std::collections::{HashMap, HashSet};
use std::future::Future;
use std::panic::AssertUnwindSafe;
use std::pin::Pin;

mod exec;
Expand Down Expand Up @@ -214,7 +215,7 @@
last_frame_time: f64,
frame_time: f64,

#[cfg(one_screenshot)]

Check warning on line 218 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 218 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 218 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 218 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 218 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`
counter: usize,

camera_stack: Vec<camera::CameraState>,
Expand Down Expand Up @@ -356,7 +357,7 @@
last_frame_time: miniquad::date::now(),
frame_time: 1. / 60.,

#[cfg(one_screenshot)]

Check warning on line 360 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 360 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 360 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 360 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 360 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`
counter: 0,
unwind: false,
recovery_future: None,
Expand Down Expand Up @@ -406,7 +407,7 @@

get_quad_context().commit_frame();

#[cfg(one_screenshot)]

Check warning on line 410 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 410 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 410 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 410 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 410 in src/lib.rs

View workflow job for this annotation

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

unexpected `cfg` condition name: `one_screenshot`
{
get_context().counter += 1;
if get_context().counter == 3 {
Expand Down Expand Up @@ -481,29 +482,21 @@
fn get_context() -> &'static mut Context {
thread_assert::same_thread();

unsafe { CONTEXT.as_mut().unwrap_or_else(|| panic!()) }

Check warning on line 485 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 485 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 485 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 485 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 485 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
}

fn get_quad_context() -> &'static mut dyn miniquad::RenderingBackend {
thread_assert::same_thread();

unsafe {
assert!(CONTEXT.is_some());

Check warning on line 492 in src/lib.rs

View workflow job for this annotation

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

creating a shared reference to mutable static is discouraged

Check warning on line 492 in src/lib.rs

View workflow job for this annotation

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

creating a shared reference to mutable static is discouraged

Check warning on line 492 in src/lib.rs

View workflow job for this annotation

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

creating a shared reference to mutable static is discouraged

Check warning on line 492 in src/lib.rs

View workflow job for this annotation

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

creating a shared reference to mutable static is discouraged

Check warning on line 492 in src/lib.rs

View workflow job for this annotation

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

creating a shared reference to mutable static is discouraged
}

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: Pin<Box<dyn Future<Output = ()>>>,
}

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

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;
}
}

Expand Down Expand Up @@ -882,9 +872,6 @@
} = 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 +880,9 @@
);
unsafe { CONTEXT = Some(context) };

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