diff --git a/src/conversion.rs b/src/conversion.rs index f82039f3..2defef3c 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -2,7 +2,6 @@ use std::borrow::Cow; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::ffi::{CStr, CString}; use std::hash::{BuildHasher, Hash}; -use std::mem::transmute; use std::os::raw::c_int; use std::string::String as StdString; use std::{slice, str}; @@ -23,14 +22,14 @@ use crate::value::{FromLua, IntoLua, Nil, Value}; impl IntoLua for Value { #[inline] fn into_lua(self, _: &Lua) -> Result { - unsafe { Ok(transmute(self)) } + Ok(self) } } impl IntoLua for &Value { #[inline] fn into_lua(self, _: &Lua) -> Result { - unsafe { Ok(transmute(self.clone())) } + Ok(self.clone()) } #[inline] @@ -49,14 +48,14 @@ impl FromLua for Value { impl IntoLua for String { #[inline] fn into_lua(self, _: &Lua) -> Result { - unsafe { Ok(Value::String(transmute(self))) } + Ok(Value::String(self)) } } impl IntoLua for &String { #[inline] fn into_lua(self, _: &Lua) -> Result { - unsafe { Ok(Value::String(transmute(self.clone()))) } + Ok(Value::String(self.clone())) } #[inline] @@ -82,14 +81,14 @@ impl FromLua for String { impl IntoLua for Table { #[inline] fn into_lua(self, _: &Lua) -> Result { - unsafe { Ok(Value::Table(transmute(self))) } + Ok(Value::Table(self)) } } impl IntoLua for &Table { #[inline] fn into_lua(self, _: &Lua) -> Result { - unsafe { Ok(Value::Table(transmute(self.clone()))) } + Ok(Value::Table(self.clone())) } #[inline] @@ -116,14 +115,14 @@ impl FromLua for Table { impl IntoLua for Function { #[inline] fn into_lua(self, _: &Lua) -> Result { - unsafe { Ok(Value::Function(transmute(self))) } + Ok(Value::Function(self)) } } impl IntoLua for &Function { #[inline] fn into_lua(self, _: &Lua) -> Result { - unsafe { Ok(Value::Function(transmute(self.clone()))) } + Ok(Value::Function(self.clone())) } #[inline] @@ -150,14 +149,14 @@ impl FromLua for Function { impl IntoLua for Thread { #[inline] fn into_lua(self, _: &Lua) -> Result { - unsafe { Ok(Value::Thread(transmute(self))) } + Ok(Value::Thread(self)) } } impl IntoLua for &Thread { #[inline] fn into_lua(self, _: &Lua) -> Result { - unsafe { Ok(Value::Thread(transmute(self.clone()))) } + Ok(Value::Thread(self.clone())) } #[inline] @@ -184,14 +183,14 @@ impl FromLua for Thread { impl IntoLua for AnyUserData { #[inline] fn into_lua(self, _: &Lua) -> Result { - unsafe { Ok(Value::UserData(transmute(self))) } + Ok(Value::UserData(self)) } } impl IntoLua for &AnyUserData { #[inline] fn into_lua(self, _: &Lua) -> Result { - unsafe { Ok(Value::UserData(transmute(self.clone()))) } + Ok(Value::UserData(self.clone())) } #[inline] @@ -359,7 +358,7 @@ impl FromLua for crate::types::Vector { impl IntoLua for StdString { #[inline] fn into_lua(self, lua: &Lua) -> Result { - Ok(Value::String(lua.create_string(&self)?)) + Ok(Value::String(lua.create_string(self)?)) } #[inline] @@ -493,7 +492,7 @@ impl IntoLua for Cow<'_, CStr> { impl IntoLua for BString { #[inline] fn into_lua(self, lua: &Lua) -> Result { - Ok(Value::String(lua.create_string(&self)?)) + Ok(Value::String(lua.create_string(self)?)) } } diff --git a/src/hook.rs b/src/hook.rs index 6179f090..03cf5549 100644 --- a/src/hook.rs +++ b/src/hook.rs @@ -29,6 +29,7 @@ pub struct Debug<'a> { enum EitherLua<'a> { Owned(ReentrantMutexGuard<'a, RawLua>), + #[cfg(not(feature = "luau"))] Borrowed(&'a RawLua), } @@ -37,7 +38,8 @@ impl Deref for EitherLua<'_> { fn deref(&self) -> &Self::Target { match self { - EitherLua::Owned(guard) => &*guard, + EitherLua::Owned(guard) => guard, + #[cfg(not(feature = "luau"))] EitherLua::Borrowed(lua) => lua, } } diff --git a/src/lib.rs b/src/lib.rs index d56321c2..ee8d51b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,8 +107,8 @@ pub use crate::chunk::{AsChunk, Chunk, ChunkMode}; pub use crate::error::{Error, ErrorContext, ExternalError, ExternalResult, Result}; pub use crate::function::{Function, FunctionInfo}; pub use crate::hook::{Debug, DebugEvent, DebugNames, DebugSource, DebugStack}; -pub use crate::state::{GCMode, Lua, LuaOptions}; pub use crate::multi::Variadic; +pub use crate::state::{GCMode, Lua, LuaOptions}; // pub use crate::scope::Scope; pub use crate::stdlib::StdLib; pub use crate::string::String; diff --git a/src/multi.rs b/src/multi.rs index 317c14e2..4f0c6632 100644 --- a/src/multi.rs +++ b/src/multi.rs @@ -1,5 +1,4 @@ use std::iter::FromIterator; -use std::mem::transmute; use std::ops::{Deref, DerefMut}; use std::os::raw::c_int; use std::result::Result as StdResult; @@ -99,7 +98,7 @@ impl FromLuaMulti for T { impl IntoLuaMulti for MultiValue { #[inline] fn into_lua_multi(self, _: &Lua) -> Result { - unsafe { Ok(transmute(self)) } + Ok(self) } } diff --git a/src/serde/mod.rs b/src/serde/mod.rs index 46c2d915..dee3a81e 100644 --- a/src/serde/mod.rs +++ b/src/serde/mod.rs @@ -5,8 +5,8 @@ use std::os::raw::c_void; use serde::{de::DeserializeOwned, ser::Serialize}; use crate::error::Result; -use crate::state::Lua; use crate::private::Sealed; +use crate::state::Lua; use crate::table::Table; use crate::util::check_stack; use crate::value::Value; diff --git a/src/state.rs b/src/state.rs index 6373ce31..903cd628 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1914,7 +1914,7 @@ impl Deref for LuaGuard { type Target = RawLua; fn deref(&self) -> &Self::Target { - &*self.0 + &self.0 } } diff --git a/src/state/extra.rs b/src/state/extra.rs index 2a075b40..37753c47 100644 --- a/src/state/extra.rs +++ b/src/state/extra.rs @@ -1,5 +1,6 @@ use std::any::TypeId; use std::cell::UnsafeCell; +use std::rc::Rc; // use std::collections::VecDeque; use std::mem::{self, MaybeUninit}; use std::os::raw::{c_int, c_void}; @@ -106,7 +107,7 @@ impl ExtraData { #[cfg(any(feature = "lua51", feature = "luajit", feature = "luau"))] pub(super) const ERROR_TRACEBACK_IDX: c_int = 1; - pub(super) unsafe fn init(state: *mut ffi::lua_State) -> Arc> { + pub(super) unsafe fn init(state: *mut ffi::lua_State) -> Rc> { // Create ref stack thread and place it in the registry to prevent it // from being garbage collected. let ref_thread = mlua_expect!( @@ -132,7 +133,7 @@ impl ExtraData { assert_eq!(ffi::lua_gettop(ref_thread), Self::ERROR_TRACEBACK_IDX); } - let extra = Arc::new(UnsafeCell::new(ExtraData { + let extra = Rc::new(UnsafeCell::new(ExtraData { lua: MaybeUninit::uninit(), weak: MaybeUninit::uninit(), registered_userdata: FxHashMap::default(), @@ -200,19 +201,19 @@ impl ExtraData { ffi::lua_pop(state, 1); return ptr::null_mut(); } - let extra_ptr = ffi::lua_touserdata(state, -1) as *mut Arc>; + let extra_ptr = ffi::lua_touserdata(state, -1) as *mut Rc>; ffi::lua_pop(state, 1); (*extra_ptr).get() } - unsafe fn store(extra: &Arc>, state: *mut ffi::lua_State) -> Result<()> { + unsafe fn store(extra: &Rc>, state: *mut ffi::lua_State) -> Result<()> { #[cfg(feature = "luau")] if cfg!(not(feature = "module")) { (*ffi::lua_callbacks(state)).userdata = extra.get() as *mut _; return Ok(()); } - push_gc_userdata(state, Arc::clone(extra), true)?; + push_gc_userdata(state, Rc::clone(extra), true)?; protect_lua!(state, 1, 0, fn(state) { let extra_key = &EXTRA_REGISTRY_KEY as *const u8 as *const c_void; ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, extra_key); diff --git a/src/state/raw.rs b/src/state/raw.rs index d47cf317..aa027de9 100644 --- a/src/state/raw.rs +++ b/src/state/raw.rs @@ -3,6 +3,7 @@ use std::cell::{Cell, UnsafeCell}; use std::ffi::{CStr, CString}; use std::os::raw::{c_char, c_int, c_void}; use std::panic::resume_unwind; +use std::rc::Rc; use std::result::Result as StdResult; use std::sync::Arc; use std::{mem, ptr}; @@ -49,7 +50,7 @@ pub struct RawLua { // The state is dynamic and depends on context pub(super) state: Cell<*mut ffi::lua_State>, pub(super) main_state: *mut ffi::lua_State, - pub(super) extra: Arc>, + pub(super) extra: Rc>, } #[cfg(not(feature = "module"))] @@ -207,10 +208,11 @@ impl RawLua { ); assert_stack(main_state, ffi::LUA_MINSTACK); + #[allow(clippy::arc_with_non_send_sync)] let rawlua = Arc::new(ReentrantMutex::new(RawLua { state: Cell::new(state), main_state, - extra: Arc::clone(&extra), + extra: Rc::clone(&extra), })); (*extra.get()).set_lua(&rawlua); @@ -1102,8 +1104,8 @@ impl RawLua { let _sg = StackGuard::new(state); check_stack(state, 4)?; - let func = mem::transmute(func); - let extra = Arc::clone(&self.extra); + let func = mem::transmute::>(func); + let extra = Rc::clone(&self.extra); let protect = !self.unlikely_memory_error(); push_gc_userdata(state, CallbackUpvalue { data: func, extra }, protect)?; if protect { @@ -1147,7 +1149,7 @@ impl RawLua { let args = MultiValue::from_stack_multi(nargs, rawlua)?; let func = &*(*upvalue).data; let fut = func(rawlua, args); - let extra = Arc::clone(&(*upvalue).extra); + let extra = Rc::clone(&(*upvalue).extra); let protect = !rawlua.unlikely_memory_error(); push_gc_userdata(state, AsyncPollUpvalue { data: fut, extra }, protect)?; if protect { @@ -1169,7 +1171,7 @@ impl RawLua { // Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments) // The lock must be already held as the future is polled let rawlua = (*extra).raw_lua(); - let _guard = StateGuard::new(&rawlua, state); + let _guard = StateGuard::new(rawlua, state); let fut = &mut (*upvalue).data; let mut ctx = Context::from_waker(rawlua.waker()); @@ -1190,7 +1192,7 @@ impl RawLua { Ok(nresults + 1) } nresults => { - let results = MultiValue::from_stack_multi(nresults, &rawlua)?; + let results = MultiValue::from_stack_multi(nresults, rawlua)?; ffi::lua_pushinteger(state, nresults as _); rawlua.push(rawlua.create_sequence_from(results)?)?; Ok(2) @@ -1206,8 +1208,8 @@ impl RawLua { let _sg = StackGuard::new(state); check_stack(state, 4)?; - let func = mem::transmute(func); - let extra = Arc::clone(&self.extra); + let func = mem::transmute::>(func); + let extra = Rc::clone(&self.extra); let protect = !self.unlikely_memory_error(); let upvalue = AsyncCallbackUpvalue { data: func, extra }; push_gc_userdata(state, upvalue, protect)?; diff --git a/src/types.rs b/src/types.rs index d23eb731..be3b0dae 100644 --- a/src/types.rs +++ b/src/types.rs @@ -3,6 +3,7 @@ use std::cell::{Cell, Ref, RefCell, RefMut, UnsafeCell}; use std::hash::{Hash, Hasher}; use std::ops::{Deref, DerefMut}; use std::os::raw::{c_int, c_void}; +use std::rc::Rc; use std::result::Result as StdResult; use std::sync::atomic::{AtomicI32, Ordering}; use std::sync::Arc; @@ -45,7 +46,7 @@ pub(crate) type Callback<'a> = Box Result + pub(crate) struct Upvalue { pub(crate) data: T, - pub(crate) extra: Arc>, + pub(crate) extra: Rc>, } pub(crate) type CallbackUpvalue = Upvalue>; diff --git a/src/userdata/cell.rs b/src/userdata/cell.rs index 33a53618..42633fd4 100644 --- a/src/userdata/cell.rs +++ b/src/userdata/cell.rs @@ -9,8 +9,8 @@ use std::rc::Rc; use serde::ser::{Serialize, Serializer}; use crate::error::{Error, Result}; -use crate::state::{Lua, LuaGuard}; use crate::state::RawLua; +use crate::state::{Lua, LuaGuard}; use crate::userdata::AnyUserData; use crate::util::get_userdata; use crate::value::{FromLua, Value}; @@ -89,22 +89,22 @@ impl UserDataVariant { } #[inline(always)] - unsafe fn get_ref(&self) -> &T { + fn as_ptr(&self) -> *mut T { match self { - Self::Default(inner) => &*inner.value.get(), + Self::Default(inner) => inner.value.get(), #[cfg(feature = "serialize")] - Self::Serializable(inner) => &*(inner.value.get() as *mut Box), + Self::Serializable(inner) => unsafe { &mut **(inner.value.get() as *mut Box) }, } } - #[inline(always)] - unsafe fn get_mut(&self) -> &mut T { - match self { - Self::Default(inner) => &mut *inner.value.get(), - #[cfg(feature = "serialize")] - Self::Serializable(inner) => &mut *(inner.value.get() as *mut Box), - } - } + // #[inline(always)] + // unsafe fn get_mut(&self) -> &mut T { + // match self { + // Self::Default(inner) => &mut *inner.value.get(), + // #[cfg(feature = "serialize")] + // Self::Serializable(inner) => &mut *(inner.value.get() as *mut Box), + // } + // } } #[cfg(feature = "serialize")] @@ -164,7 +164,7 @@ impl Deref for UserDataRef { #[inline] fn deref(&self) -> &T { - unsafe { self.variant.get_ref() } + unsafe { &*self.variant.as_ptr() } } } @@ -226,14 +226,14 @@ impl Deref for UserDataRefMut { #[inline] fn deref(&self) -> &Self::Target { - unsafe { self.variant.get_ref() } + unsafe { &*self.variant.as_ptr() } } } impl DerefMut for UserDataRefMut { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { - unsafe { self.variant.get_mut() } + unsafe { &mut *self.variant.as_ptr() } } } @@ -348,7 +348,7 @@ impl<'a, T> Deref for UserDataBorrowRef<'a, T> { #[inline] fn deref(&self) -> &T { - unsafe { self.0.get_ref() } + unsafe { &*self.0.as_ptr() } } } @@ -366,7 +366,7 @@ impl<'a, T> UserDataBorrowRef<'a, T> { #[inline(always)] pub(crate) fn get_ref(&self) -> &'a T { // SAFETY: `UserDataBorrowRef` is only created when the borrow flag is set to reading. - unsafe { self.0.get_ref() } + unsafe { &*self.0.as_ptr() } } } @@ -384,14 +384,14 @@ impl<'a, T> Deref for UserDataBorrowMut<'a, T> { #[inline] fn deref(&self) -> &T { - unsafe { self.0.get_ref() } + unsafe { &*self.0.as_ptr() } } } impl<'a, T> DerefMut for UserDataBorrowMut<'a, T> { #[inline] fn deref_mut(&mut self) -> &mut T { - unsafe { self.0.get_mut() } + unsafe { &mut *self.0.as_ptr() } } } @@ -409,7 +409,7 @@ impl<'a, T> UserDataBorrowMut<'a, T> { #[inline(always)] pub(crate) fn get_mut(&mut self) -> &'a mut T { // SAFETY: `UserDataBorrowMut` is only created when the borrow flag is set to writing. - unsafe { self.0.get_mut() } + unsafe { &mut *self.0.as_ptr() } } } diff --git a/src/userdata/registry.rs b/src/userdata/registry.rs index 6cb9a40f..90282c10 100644 --- a/src/userdata/registry.rs +++ b/src/userdata/registry.rs @@ -166,7 +166,7 @@ impl<'a, T: 'static> UserDataRegistry<'a, T> { Err(e) => return Box::pin(future::ready(Err(e))), }; let fut = method(lua, ud.get_ref(), args); - Box::pin(async move { fut.await?.push_into_stack_multi(&rawlua) }) + Box::pin(async move { fut.await?.push_into_stack_multi(rawlua) }) } _ => { let err = Error::bad_self_argument(&name, Error::UserDataTypeMismatch); @@ -213,7 +213,7 @@ impl<'a, T: 'static> UserDataRegistry<'a, T> { Err(e) => return Box::pin(future::ready(Err(e))), }; let fut = method(lua, ud.get_mut(), args); - Box::pin(async move { fut.await?.push_into_stack_multi(&rawlua) }) + Box::pin(async move { fut.await?.push_into_stack_multi(rawlua) }) } _ => { let err = Error::bad_self_argument(&name, Error::UserDataTypeMismatch); @@ -261,7 +261,7 @@ impl<'a, T: 'static> UserDataRegistry<'a, T> { FR: Future> + 'a, R: IntoLuaMulti, { - let name = get_function_name::(&name); + let name = get_function_name::(name); Box::new(move |rawlua, args| unsafe { let lua = rawlua.lua(); let args = match A::from_lua_args(args, 1, Some(&name), lua) { @@ -269,7 +269,7 @@ impl<'a, T: 'static> UserDataRegistry<'a, T> { Err(e) => return Box::pin(future::ready(Err(e))), }; let fut = function(lua, args); - Box::pin(async move { fut.await?.push_into_stack_multi(&rawlua) }) + Box::pin(async move { fut.await?.push_into_stack_multi(rawlua) }) }) } @@ -315,8 +315,8 @@ impl<'a, T: 'static> UserDataFields<'a, T> for UserDataRegistry<'a, T> { R: IntoLua, { let name = name.to_string(); - let callback = Self::box_method(&name, move |lua, data, ()| method(lua, &data)); - self.field_getters.push((name.into(), callback)); + let callback = Self::box_method(&name, move |lua, data, ()| method(lua, data)); + self.field_getters.push((name, callback)); } fn add_field_method_set(&mut self, name: impl ToString, method: M) diff --git a/src/util/mod.rs b/src/util/mod.rs index 1fb81ecf..966a919a 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -6,6 +6,7 @@ use std::fmt::Write; use std::mem::MaybeUninit; use std::os::raw::{c_char, c_int, c_void}; use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe}; +use std::rc::Rc; use std::sync::Arc; use std::{ptr, slice, str}; @@ -20,7 +21,7 @@ pub(crate) use short_names::short_type_name; static METATABLE_CACHE: Lazy> = Lazy::new(|| { let mut map = FxHashMap::with_capacity_and_hasher(32, Default::default()); - map.insert(TypeId::of::>>(), 0); + map.insert(TypeId::of::>>(), 0); map.insert(TypeId::of::(), 0); map.insert(TypeId::of::(), 0);