From 5ab97666d726b67d97b1b89bf925e63e917ca167 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Fri, 7 Mar 2025 21:21:14 +0000 Subject: [PATCH] Fix clippy warnings --- Cargo.toml | 1 + src/memory.rs | 26 +++++++++++++++++++++----- src/state.rs | 4 ++-- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0481871..91f373b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,7 @@ erased-serde = { version = "0.4", optional = true } serde-value = { version = "0.7", optional = true } parking_lot = { version = "0.12", features = ["arc_lock"] } anyhow = { version = "1.0", optional = true } +rustversion = "1.0" ffi = { package = "mlua-sys", version = "0.6.6", path = "mlua-sys" } diff --git a/src/memory.rs b/src/memory.rs index 672a864..92c6032 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -18,15 +18,31 @@ pub(crate) struct MemoryState { } impl MemoryState { + #[cfg(feature = "luau")] #[inline] pub(crate) unsafe fn get(state: *mut ffi::lua_State) -> *mut Self { let mut mem_state = ptr::null_mut(); - #[cfg(feature = "luau")] - { - ffi::lua_getallocf(state, &mut mem_state); - mlua_assert!(!mem_state.is_null(), "Luau state has no allocator userdata"); + ffi::lua_getallocf(state, &mut mem_state); + mlua_assert!(!mem_state.is_null(), "Luau state has no allocator userdata"); + mem_state as *mut MemoryState + } + + #[cfg(not(feature = "luau"))] + #[rustversion::since(1.85)] + #[inline] + pub(crate) unsafe fn get(state: *mut ffi::lua_State) -> *mut Self { + let mut mem_state = ptr::null_mut(); + if !ptr::fn_addr_eq(ffi::lua_getallocf(state, &mut mem_state), ALLOCATOR) { + mem_state = ptr::null_mut(); } - #[cfg(not(feature = "luau"))] + mem_state as *mut MemoryState + } + + #[cfg(not(feature = "luau"))] + #[rustversion::before(1.85)] + #[inline] + pub(crate) unsafe fn get(state: *mut ffi::lua_State) -> *mut Self { + let mut mem_state = ptr::null_mut(); if ffi::lua_getallocf(state, &mut mem_state) != ALLOCATOR { mem_state = ptr::null_mut(); } diff --git a/src/state.rs b/src/state.rs index 1054fbf..fb0bda4 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1809,8 +1809,8 @@ impl Lua { let state = lua.state(); unsafe { let mut unref_list = (*lua.extra.get()).registry_unref_list.lock(); - let unref_list = mem::replace(&mut *unref_list, Some(Vec::new())); - for id in mlua_expect!(unref_list, "unref list not set") { + let unref_list = unref_list.replace(Vec::new()); + for id in mlua_expect!(unref_list, "unref list is not set") { ffi::luaL_unref(state, ffi::LUA_REGISTRYINDEX, id); } }