From d4ca16f07e68130a182ac44ca755e640c61b60ec Mon Sep 17 00:00:00 2001 From: meehl <12648828+meehl@users.noreply.github.com> Date: Thu, 20 Aug 2026 10:38:46 +0200 Subject: [PATCH] Remove unnecessary Box around Context --- src/api.rs | 6 +++--- src/api/clipboard.rs | 4 ++-- src/api/image_handle.rs | 2 +- src/api/input.rs | 4 ++-- src/api/lua.rs | 4 ++-- src/api/paths.rs | 8 ++++---- src/api/rendering.rs | 18 +++++++++--------- src/api/window.rs | 12 ++++++------ src/lua.rs | 14 +++++++------- 9 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/api.rs b/src/api.rs index 169ddbe..262c938 100644 --- a/src/api.rs +++ b/src/api.rs @@ -135,13 +135,13 @@ fn exit(l: &Lua, exit_msg: Option) -> LuaResult<()> { if let Some(exit_msg) = exit_msg { println!("{exit_msg}"); } - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); *ctx.should_exit() = true; Ok(()) } fn restart(l: &Lua, _: ()) -> LuaResult<()> { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); *ctx.needs_restart() = true; Ok(()) } @@ -154,7 +154,7 @@ fn open_url(l: &Lua, url: String) -> LuaResult { } fn render_init(l: &Lua, features: Variadic) -> LuaResult<()> { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); for feature in features { if feature == "DPI_AWARE" { *ctx.is_dpi_aware() = true; diff --git a/src/api/clipboard.rs b/src/api/clipboard.rs index 6937dcb..b5a0d52 100644 --- a/src/api/clipboard.rs +++ b/src/api/clipboard.rs @@ -2,12 +2,12 @@ use crate::lua::Context; use mlua::{Lua, Result as LuaResult}; pub fn copy(l: &Lua, text: String) -> LuaResult<()> { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); ctx.window().set_clipboard_text(text); Ok(()) } pub fn paste(l: &Lua, _: ()) -> LuaResult> { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); Ok(ctx.window().get_clipboard_text()) } diff --git a/src/api/image_handle.rs b/src/api/image_handle.rs index 457eb5f..6c33310 100644 --- a/src/api/image_handle.rs +++ b/src/api/image_handle.rs @@ -178,7 +178,7 @@ unsafe extern "C-unwind" fn image_handle_load(state: *mut ffi::lua_State) -> c_i let handle = unsafe { get_image_handle(state, 1) }; let lua = unsafe { Lua::get_or_init_from_ptr(state) }; - let ctx = lua.app_data_ref::<&'static Context>().unwrap(); + let ctx = lua.app_data_ref::().unwrap(); // Path let path_ptr = unsafe { ffi::luaL_checklstring(state, 2, ptr::null_mut()) }; diff --git a/src/api/input.rs b/src/api/input.rs index 827a4ff..5a74ea0 100644 --- a/src/api/input.rs +++ b/src/api/input.rs @@ -5,13 +5,13 @@ use crate::{ use mlua::{Lua, Result as LuaResult}; pub fn get_cursor_pos(l: &Lua, _: ()) -> LuaResult<(u32, u32)> { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); let pos = ctx.input().mouse_pos(); Ok((pos.x as u32, pos.y as u32)) } pub fn is_key_down(l: &Lua, key_name: String) -> LuaResult { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); if let Some(key) = str_as_key(&key_name) { Ok(ctx.input().key_pressed(key)) diff --git a/src/api/lua.rs b/src/api/lua.rs index bc19b0d..507a7c9 100644 --- a/src/api/lua.rs +++ b/src/api/lua.rs @@ -12,7 +12,7 @@ pub fn protected_call(l: &Lua, (func, args): (Function, MultiValue)) -> LuaResul } pub fn load_module(l: &Lua, (name, args): (String, MultiValue)) -> LuaResult { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); let mut module_path = ctx.script_dir().join(name); if module_path.extension().is_none() { module_path.set_extension("lua"); @@ -26,7 +26,7 @@ pub fn load_module(l: &Lua, (name, args): (String, MultiValue)) -> LuaResult LuaResult { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); let mut module_path = ctx.script_dir().join(name); if module_path.extension().is_none() { module_path.set_extension("lua"); diff --git a/src/api/paths.rs b/src/api/paths.rs index e7bede5..254d1aa 100644 --- a/src/api/paths.rs +++ b/src/api/paths.rs @@ -7,13 +7,13 @@ use crate::{ }; pub fn get_user_path(l: &Lua, _: ()) -> LuaResult { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); Ok(ctx.script_dir().join("userdata")) } // parent directory of Launch.lua script pub fn get_script_path(l: &Lua, _: ()) -> LuaResult { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); Ok(ctx.script_dir().to_owned()) } @@ -26,13 +26,13 @@ pub fn get_runtime_path(_: &Lua, _: ()) -> LuaResult { } pub fn get_work_dir(l: &Lua, _: ()) -> LuaResult { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); Ok(ctx.current_working_dir().clone()) } // NOTE: unused pub fn set_work_dir(l: &Lua, path: String) -> LuaResult<()> { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); if change_working_directory(&path).is_ok() { *ctx.current_working_dir() = path.into(); } diff --git a/src/api/rendering.rs b/src/api/rendering.rs index 6cbb2d9..104a789 100644 --- a/src/api/rendering.rs +++ b/src/api/rendering.rs @@ -80,7 +80,7 @@ macro_rules! i32_from_stack { unsafe extern "C-unwind" fn set_draw_color(state: *mut ffi::lua_State) -> c_int { //profiling::scope!("set_draw_color"); let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) }; - let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap(); + let ctx = lua_instance.app_data_ref::().unwrap(); let nargs = unsafe { ffi::lua_gettop(state) }; match nargs { @@ -116,7 +116,7 @@ unsafe extern "C-unwind" fn set_draw_color(state: *mut ffi::lua_State) -> c_int unsafe extern "C-unwind" fn get_draw_color(state: *mut ffi::lua_State) -> c_int { //profiling::scope!("get_draw_color"); let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) }; - let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap(); + let ctx = lua_instance.app_data_ref::().unwrap(); let color: [f32; 4] = ctx.recorder().get_draw_color().into(); unsafe { ffi::lua_pushnumber(state, color[0] as f64) }; @@ -130,7 +130,7 @@ unsafe extern "C-unwind" fn get_draw_color(state: *mut ffi::lua_State) -> c_int unsafe extern "C-unwind" fn set_viewport(state: *mut ffi::lua_State) -> c_int { //profiling::scope!("set_viewport"); let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) }; - let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap(); + let ctx = lua_instance.app_data_ref::().unwrap(); let nargs = unsafe { ffi::lua_gettop(state) }; match nargs { @@ -154,7 +154,7 @@ unsafe extern "C-unwind" fn set_viewport(state: *mut ffi::lua_State) -> c_int { unsafe extern "C-unwind" fn set_draw_layer(state: *mut ffi::lua_State) -> c_int { //profiling::scope!("set_draw_layer"); let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) }; - let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap(); + let ctx = lua_instance.app_data_ref::().unwrap(); let nargs = unsafe { ffi::lua_gettop(state) }; @@ -188,7 +188,7 @@ unsafe extern "C-unwind" fn set_draw_layer(state: *mut ffi::lua_State) -> c_int unsafe extern "C-unwind" fn draw_image(state: *mut ffi::lua_State) -> c_int { //profiling::scope!("draw_image"); let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) }; - let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap(); + let ctx = lua_instance.app_data_ref::().unwrap(); let nargs = unsafe { ffi::lua_gettop(state) }; if !matches!(nargs, 5 | 6 | 7 | 9 | 10 | 11) { @@ -236,7 +236,7 @@ unsafe extern "C-unwind" fn draw_image(state: *mut ffi::lua_State) -> c_int { unsafe extern "C-unwind" fn draw_image_quad(state: *mut ffi::lua_State) -> c_int { //profiling::scope!("draw_image_quad"); let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) }; - let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap(); + let ctx = lua_instance.app_data_ref::().unwrap(); let nargs = unsafe { ffi::lua_gettop(state) }; if !matches!(nargs, 9 | 10 | 11 | 17 | 18 | 19) { @@ -303,7 +303,7 @@ unsafe extern "C-unwind" fn draw_image_quad(state: *mut ffi::lua_State) -> c_int unsafe extern "C-unwind" fn draw_string(state: *mut ffi::lua_State) -> c_int { //profiling::scope!("draw_string"); let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) }; - let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap(); + let ctx = lua_instance.app_data_ref::().unwrap(); let nargs = unsafe { ffi::lua_gettop(state) }; @@ -380,7 +380,7 @@ unsafe extern "C-unwind" fn draw_string(state: *mut ffi::lua_State) -> c_int { unsafe extern "C-unwind" fn get_string_width(state: *mut ffi::lua_State) -> c_int { //profiling::scope!("get_string_width"); let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) }; - let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap(); + let ctx = lua_instance.app_data_ref::().unwrap(); let nargs = unsafe { ffi::lua_gettop(state) }; @@ -405,7 +405,7 @@ fn get_index_at_cur( (line_height, font_type, text, cur_x, cur_y): (i32, String, String, f32, f32), ) -> LuaResult { //profiling::scope!("get_char_index_at_cur"); - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); let font_type = font_type.parse::()?; diff --git a/src/api/window.rs b/src/api/window.rs index c653ac2..cac5922 100644 --- a/src/api/window.rs +++ b/src/api/window.rs @@ -5,7 +5,7 @@ use crate::{ use mlua::{Lua, Result as LuaResult}; pub fn get_screen_size(l: &Lua, _: ()) -> LuaResult<(u32, u32)> { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); let size = if *ctx.is_dpi_aware() { let PhysicalSize { width, height, .. } = ctx.window().size; (width, height) @@ -17,25 +17,25 @@ pub fn get_screen_size(l: &Lua, _: ()) -> LuaResult<(u32, u32)> { } pub fn get_screen_scale(l: &Lua, _: ()) -> LuaResult { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); let scale_factor = ctx.window().scale_factor(); Ok(scale_factor) } pub fn set_window_title(l: &Lua, title: String) -> LuaResult<()> { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); ctx.window().set_window_title(&title); Ok(()) } pub fn set_foreground(l: &Lua, _: ()) -> LuaResult<()> { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); ctx.window().focus(); Ok(()) } pub fn set_dpi_scale_override(l: &Lua, percent: i32) -> LuaResult<()> { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); match percent { 0 => ctx.window().scale_factor_override = None, p if p > 0 => ctx.window().scale_factor_override = Some(p as f32 / 100.0), @@ -45,7 +45,7 @@ pub fn set_dpi_scale_override(l: &Lua, percent: i32) -> LuaResult<()> { } pub fn get_dpi_scale_override(l: &Lua, _: ()) -> LuaResult { - let ctx = l.app_data_ref::<&'static Context>().unwrap(); + let ctx = l.app_data_ref::().unwrap(); match ctx.window().scale_factor_override { Some(scale_factor) => Ok((scale_factor * 100.0) as i32), None => Ok(0), diff --git a/src/lua.rs b/src/lua.rs index f66600a..4c77e5b 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -57,8 +57,8 @@ pub struct Context { } impl Context { - pub fn new() -> &'static Self { - Box::leak(Box::new(Self { + pub fn new() -> Self { + Self { window: Cell::new(std::ptr::null_mut()), input: Cell::new(std::ptr::null()), fonts: Cell::new(std::ptr::null_mut()), @@ -69,7 +69,7 @@ impl Context { needs_restart: Cell::new(std::ptr::null_mut()), should_exit: Cell::new(std::ptr::null_mut()), is_dpi_aware: Cell::new(std::ptr::null_mut()), - })) + } } pub fn set(&self, ctx: &mut PoBContext) { @@ -199,7 +199,7 @@ impl LuaInstance { /// Loads and executes PoB's Launch.lua script pub fn launch(&self, pob_ctx: &mut PoBContext) -> LuaResult<()> { - let ctx = self.lua.app_data_ref::<&'static Context>().unwrap(); + let ctx = self.lua.app_data_ref::().unwrap(); ctx.set(pob_ctx); let script_dir = pob_ctx.app.script_dir.as_path(); @@ -221,7 +221,7 @@ impl LuaInstance { pub fn handle_subscripts(&self, pob_ctx: &mut PoBContext) { profiling::scope!("handle_subscripts"); - let ctx = self.lua.app_data_ref::<&'static Context>().unwrap(); + let ctx = self.lua.app_data_ref::().unwrap(); ctx.set(pob_ctx); let subscript_events = self.subscript_manager.borrow_mut().process(self); ctx.clear(); @@ -265,7 +265,7 @@ impl LuaInstance { } pub fn can_exit(&self, pob_ctx: &mut PoBContext) -> bool { - let ctx = self.lua.app_data_ref::<&'static Context>().unwrap(); + let ctx = self.lua.app_data_ref::().unwrap(); ctx.set(pob_ctx); let can_exit = get_callback(&self.lua, "CanExit") @@ -280,7 +280,7 @@ impl LuaInstance { profiling::scope!("handle_event", format!("{}", event)); // "Plug" references into context - let ctx = self.lua.app_data_ref::<&'static Context>().unwrap(); + let ctx = self.lua.app_data_ref::().unwrap(); ctx.set(pob_ctx); // Call event handler in PoB application code