diff --git a/src/lua.rs b/src/lua.rs index 29639d3..50d16b7 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -2493,23 +2493,15 @@ impl Lua { } } - pub(crate) fn drop_ref(&self, lref: &LuaRef) { + pub(crate) fn drop_ref_index(&self, index: c_int) { unsafe { let ref_thread = self.ref_thread(); ffi::lua_pushnil(ref_thread); - ffi::lua_replace(ref_thread, lref.index); - (*self.extra.get()).ref_free.push(lref.index); + ffi::lua_replace(ref_thread, index); + (*self.extra.get()).ref_free.push(index); } } - #[cfg(feature = "unstable")] - pub(crate) fn make_owned_ref(&self, lref: LuaRef) -> crate::types::LuaOwnedRef { - assert!(lref.drop, "Cannot turn non-drop reference into owned"); - let owned_ref = crate::types::LuaOwnedRef::new(Lua(self.0.clone()), lref.index); - mem::forget(lref); - owned_ref - } - #[cfg(feature = "unstable")] pub(crate) fn adopt_owned_ref(&self, loref: crate::types::LuaOwnedRef) -> LuaRef { assert!( @@ -3041,6 +3033,12 @@ impl Lua { .map(|x| x.as_ref().memory_limit == 0) .unwrap_or_default() } + + #[cfg(feature = "unstable")] + #[inline] + pub(crate) fn clone(&self) -> Self { + Lua(Arc::clone(&self.0)) + } } impl LuaInner { diff --git a/src/types.rs b/src/types.rs index ace1bf6..47bd9db 100644 --- a/src/types.rs +++ b/src/types.rs @@ -195,7 +195,10 @@ impl<'lua> LuaRef<'lua> { #[cfg(feature = "unstable")] #[inline] pub(crate) fn into_owned(self) -> LuaOwnedRef { - self.lua.make_owned_ref(self) + assert!(self.drop, "Cannot turn non-drop reference into owned"); + let owned_ref = LuaOwnedRef::new(self.lua.clone(), self.index); + mem::forget(self); + owned_ref } } @@ -214,7 +217,7 @@ impl<'lua> Clone for LuaRef<'lua> { impl<'lua> Drop for LuaRef<'lua> { fn drop(&mut self) { if self.drop { - self.lua.drop_ref(self); + self.lua.drop_ref_index(self.index); } } } @@ -250,18 +253,14 @@ impl fmt::Debug for LuaOwnedRef { #[cfg(feature = "unstable")] impl Clone for LuaOwnedRef { fn clone(&self) -> Self { - self.lua.make_owned_ref(self.to_ref().clone()) + self.to_ref().clone().into_owned() } } #[cfg(feature = "unstable")] impl Drop for LuaOwnedRef { fn drop(&mut self) { - drop(LuaRef { - lua: &self.lua, - index: self.index, - drop: true, - }); + self.lua.drop_ref_index(self.index); } }