diff --git a/src/function.rs b/src/function.rs index 9918ca6..39b9fce 100644 --- a/src/function.rs +++ b/src/function.rs @@ -7,7 +7,7 @@ use std::slice; use crate::error::{Error, Result}; use crate::lua::Lua; use crate::table::Table; -use crate::types::{Callback, LuaRef, MaybeSend}; +use crate::types::{Callback, MaybeSend, ValueRef}; use crate::util::{ assert_stack, check_stack, linenumber_to_usize, pop_error, ptr_to_lossy_str, ptr_to_str, StackGuard, @@ -22,7 +22,7 @@ use { /// Handle to an internal Lua function. #[derive(Clone, Debug)] -pub struct Function<'lua>(pub(crate) LuaRef<'lua>); +pub struct Function<'lua>(pub(crate) ValueRef<'lua>); /// Owned handle to an internal Lua function. /// @@ -34,7 +34,7 @@ pub struct Function<'lua>(pub(crate) LuaRef<'lua>); #[cfg(feature = "unstable")] #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))] #[derive(Clone, Debug)] -pub struct OwnedFunction(pub(crate) crate::types::LuaOwnedRef); +pub struct OwnedFunction(pub(crate) crate::types::OwnedValueRef); #[cfg(feature = "unstable")] impl OwnedFunction { diff --git a/src/lua.rs b/src/lua.rs index 8d165d7..f47c945 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -26,7 +26,7 @@ use crate::table::Table; use crate::thread::Thread; use crate::types::{ AppData, AppDataRef, AppDataRefMut, Callback, CallbackUpvalue, DestructedUserdata, Integer, - LightUserData, LuaRef, MaybeSend, Number, RegistryKey, SubtypeId, + LightUserData, MaybeSend, Number, RegistryKey, SubtypeId, ValueRef, }; use crate::userdata::{AnyUserData, MetaMethod, UserData, UserDataCell}; use crate::userdata_impl::{UserDataProxy, UserDataRegistry}; @@ -1691,7 +1691,7 @@ impl Lua { ffi::lua_replace(thread_state, ffi::LUA_GLOBALSINDEX); } - return Ok(Thread::new(LuaRef::new(self, index))); + return Ok(Thread::new(ValueRef::new(self, index))); } }; self.create_thread_inner(func) @@ -2575,26 +2575,26 @@ impl Lua { } } - // Pushes a LuaRef value onto the stack, uses 1 stack space, does not call checkstack - pub(crate) unsafe fn push_ref(&self, lref: &LuaRef) { + // Pushes a ValueRef value onto the stack, uses 1 stack space, does not call checkstack + pub(crate) unsafe fn push_ref(&self, vref: &ValueRef) { assert!( - Arc::ptr_eq(&lref.lua.0, &self.0), + Arc::ptr_eq(&vref.lua.0, &self.0), "Lua instance passed Value created from a different main Lua state" ); - ffi::lua_xpush(self.ref_thread(), self.state(), lref.index); + ffi::lua_xpush(self.ref_thread(), self.state(), vref.index); } #[cfg(all(feature = "unstable", not(feature = "send")))] - pub(crate) unsafe fn push_owned_ref(&self, loref: &crate::types::LuaOwnedRef) { + pub(crate) unsafe fn push_owned_ref(&self, vref: &crate::types::OwnedValueRef) { assert!( - Arc::ptr_eq(&loref.inner, &self.0), + Arc::ptr_eq(&vref.inner, &self.0), "Lua instance passed Value created from a different main Lua state" ); - ffi::lua_xpush(self.ref_thread(), self.state(), loref.index); + ffi::lua_xpush(self.ref_thread(), self.state(), vref.index); } // Pops the topmost element of the stack and stores a reference to it. This pins the object, - // preventing garbage collection until the returned `LuaRef` is dropped. + // preventing garbage collection until the returned `ValueRef` is dropped. // // References are stored in the stack of a specially created auxiliary thread that exists only // to store reference values. This is much faster than storing these in the registry, and also @@ -2602,23 +2602,23 @@ impl Lua { // used stack. The implementation is somewhat biased towards the use case of a relatively small // number of short term references being created, and `RegistryKey` being used for long term // references. - pub(crate) unsafe fn pop_ref(&self) -> LuaRef { + pub(crate) unsafe fn pop_ref(&self) -> ValueRef { ffi::lua_xmove(self.state(), self.ref_thread(), 1); let index = ref_stack_pop(self.extra.get()); - LuaRef::new(self, index) + ValueRef::new(self, index) } // Same as `pop_ref` but assumes the value is already on the reference thread - pub(crate) unsafe fn pop_ref_thread(&self) -> LuaRef { + pub(crate) unsafe fn pop_ref_thread(&self) -> ValueRef { let index = ref_stack_pop(self.extra.get()); - LuaRef::new(self, index) + ValueRef::new(self, index) } - pub(crate) fn clone_ref(&self, lref: &LuaRef) -> LuaRef { + pub(crate) fn clone_ref(&self, vref: &ValueRef) -> ValueRef { unsafe { - ffi::lua_pushvalue(self.ref_thread(), lref.index); + ffi::lua_pushvalue(self.ref_thread(), vref.index); let index = ref_stack_pop(self.extra.get()); - LuaRef::new(self, index) + ValueRef::new(self, index) } } @@ -2632,17 +2632,17 @@ impl Lua { } #[cfg(all(feature = "unstable", not(feature = "send")))] - pub(crate) fn adopt_owned_ref(&self, loref: crate::types::LuaOwnedRef) -> LuaRef { + pub(crate) fn adopt_owned_ref(&self, vref: crate::types::OwnedValueRef) -> ValueRef { assert!( - Arc::ptr_eq(&loref.inner, &self.0), + Arc::ptr_eq(&vref.inner, &self.0), "Lua instance passed Value created from a different main Lua state" ); - let index = loref.index; + let index = vref.index; unsafe { - ptr::read(&loref.inner); - mem::forget(loref); + ptr::read(&vref.inner); + mem::forget(vref); } - LuaRef::new(self, index) + ValueRef::new(self, index) } #[inline] @@ -2835,11 +2835,14 @@ impl Lua { } } - // Returns `TypeId` for the `lref` userdata, checking that it's registered and not destructed. + // Returns `TypeId` for the userdata ref, checking that it's registered and not destructed. // // Returns `None` if the userdata is registered but non-static. - pub(crate) unsafe fn get_userdata_ref_type_id(&self, lref: &LuaRef) -> Result> { - self.get_userdata_type_id_inner(self.ref_thread(), lref.index) + pub(crate) unsafe fn get_userdata_ref_type_id( + &self, + vref: &ValueRef, + ) -> Result> { + self.get_userdata_type_id_inner(self.ref_thread(), vref.index) } // Same as `get_userdata_ref_type_id` but assumes the userdata is already on the stack. @@ -2876,11 +2879,11 @@ impl Lua { } } - // Pushes a LuaRef (userdata) value onto the stack, returning their `TypeId`. + // Pushes a ValueRef (userdata) value onto the stack, returning their `TypeId`. // Uses 1 stack space, does not call checkstack. - pub(crate) unsafe fn push_userdata_ref(&self, lref: &LuaRef) -> Result> { - let type_id = self.get_userdata_type_id_inner(self.ref_thread(), lref.index)?; - self.push_ref(lref); + pub(crate) unsafe fn push_userdata_ref(&self, vref: &ValueRef) -> Result> { + let type_id = self.get_userdata_type_id_inner(self.ref_thread(), vref.index)?; + self.push_ref(vref); Ok(type_id) } diff --git a/src/scope.rs b/src/scope.rs index 004324e..fddbf36 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -10,7 +10,7 @@ use serde::Serialize; use crate::error::{Error, Result}; use crate::function::Function; use crate::lua::Lua; -use crate::types::{Callback, CallbackUpvalue, LuaRef, MaybeSend, SubtypeId}; +use crate::types::{Callback, CallbackUpvalue, MaybeSend, SubtypeId, ValueRef}; use crate::userdata::{ AnyUserData, MetaMethod, UserData, UserDataCell, UserDataFields, UserDataMethods, }; @@ -38,11 +38,11 @@ where 'lua: 'scope, { lua: &'lua Lua, - destructors: RefCell, DestructorCallback<'lua>)>>, + destructors: RefCell, DestructorCallback<'lua>)>>, _scope_invariant: PhantomData>, } -type DestructorCallback<'lua> = Box) -> Vec> + 'lua>; +type DestructorCallback<'lua> = Box) -> Vec> + 'lua>; impl<'lua, 'scope> Scope<'lua, 'scope> { pub(crate) fn new(lua: &'lua Lua) -> Scope<'lua, 'scope> { diff --git a/src/string.rs b/src/string.rs index 3b805d5..add8bdc 100644 --- a/src/string.rs +++ b/src/string.rs @@ -11,13 +11,13 @@ use { }; use crate::error::{Error, Result}; -use crate::types::LuaRef; +use crate::types::ValueRef; /// Handle to an internal Lua string. /// /// Unlike Rust strings, Lua strings may not be valid UTF-8. #[derive(Clone)] -pub struct String<'lua>(pub(crate) LuaRef<'lua>); +pub struct String<'lua>(pub(crate) ValueRef<'lua>); /// Owned handle to an internal Lua string. /// @@ -29,7 +29,7 @@ pub struct String<'lua>(pub(crate) LuaRef<'lua>); #[cfg(feature = "unstable")] #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))] #[derive(Clone)] -pub struct OwnedString(pub(crate) crate::types::LuaOwnedRef); +pub struct OwnedString(pub(crate) crate::types::OwnedValueRef); #[cfg(feature = "unstable")] impl OwnedString { diff --git a/src/table.rs b/src/table.rs index 5d7decf..43b942e 100644 --- a/src/table.rs +++ b/src/table.rs @@ -13,7 +13,7 @@ use { use crate::error::{Error, Result}; use crate::function::Function; use crate::private::Sealed; -use crate::types::{Integer, LuaRef}; +use crate::types::{Integer, ValueRef}; use crate::util::{assert_stack, check_stack, StackGuard}; use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, Nil, Value}; @@ -22,7 +22,7 @@ use futures_util::future::{self, LocalBoxFuture}; /// Handle to an internal Lua table. #[derive(Clone)] -pub struct Table<'lua>(pub(crate) LuaRef<'lua>); +pub struct Table<'lua>(pub(crate) ValueRef<'lua>); /// Owned handle to an internal Lua table. /// @@ -34,7 +34,7 @@ pub struct Table<'lua>(pub(crate) LuaRef<'lua>); #[cfg(feature = "unstable")] #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))] #[derive(Clone, Debug)] -pub struct OwnedTable(pub(crate) crate::types::LuaOwnedRef); +pub struct OwnedTable(pub(crate) crate::types::OwnedValueRef); #[cfg(feature = "unstable")] impl OwnedTable { @@ -1158,7 +1158,7 @@ impl<'a, 'lua> Serialize for SerializableTable<'a, 'lua> { /// /// [`Table::pairs`]: crate::Table::pairs pub struct TablePairs<'lua, K, V> { - table: LuaRef<'lua>, + table: ValueRef<'lua>, key: Option>, _phantom: PhantomData<(K, V)>, } @@ -1218,7 +1218,7 @@ where /// [`Table::sequence_values`]: crate::Table::sequence_values pub struct TableSequence<'lua, V> { // TODO: Use `&Table` - table: LuaRef<'lua>, + table: ValueRef<'lua>, index: Integer, _phantom: PhantomData, } diff --git a/src/thread.rs b/src/thread.rs index 4c003a7..f90daba 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -3,7 +3,7 @@ use std::os::raw::{c_int, c_void}; use crate::error::{Error, Result}; #[allow(unused)] use crate::lua::Lua; -use crate::types::LuaRef; +use crate::types::ValueRef; use crate::util::{check_stack, error_traceback_thread, pop_error, StackGuard}; use crate::value::{FromLuaMulti, IntoLuaMulti}; @@ -43,7 +43,7 @@ pub enum ThreadStatus { /// Handle to an internal Lua thread (coroutine). #[derive(Clone, Debug)] -pub struct Thread<'lua>(pub(crate) LuaRef<'lua>, pub(crate) *mut ffi::lua_State); +pub struct Thread<'lua>(pub(crate) ValueRef<'lua>, pub(crate) *mut ffi::lua_State); /// Owned handle to an internal Lua thread (coroutine). /// @@ -56,7 +56,7 @@ pub struct Thread<'lua>(pub(crate) LuaRef<'lua>, pub(crate) *mut ffi::lua_State) #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))] #[derive(Clone, Debug)] pub struct OwnedThread( - pub(crate) crate::types::LuaOwnedRef, + pub(crate) crate::types::OwnedValueRef, pub(crate) *mut ffi::lua_State, ); @@ -87,7 +87,7 @@ pub struct AsyncThread<'lua, R> { impl<'lua> Thread<'lua> { #[inline(always)] - pub(crate) fn new(r#ref: LuaRef<'lua>) -> Self { + pub(crate) fn new(r#ref: ValueRef<'lua>) -> Self { let state = unsafe { ffi::lua_tothread(r#ref.lua.ref_thread(), r#ref.index) }; Thread(r#ref, state) } diff --git a/src/types.rs b/src/types.rs index a87fdf6..aa8ccad 100644 --- a/src/types.rs +++ b/src/types.rs @@ -273,15 +273,15 @@ impl RegistryKey { } } -pub(crate) struct LuaRef<'lua> { +pub(crate) struct ValueRef<'lua> { pub(crate) lua: &'lua Lua, pub(crate) index: c_int, pub(crate) drop: bool, } -impl<'lua> LuaRef<'lua> { +impl<'lua> ValueRef<'lua> { pub(crate) const fn new(lua: &'lua Lua, index: c_int) -> Self { - LuaRef { + ValueRef { lua, index, drop: true, @@ -295,27 +295,27 @@ impl<'lua> LuaRef<'lua> { #[cfg(feature = "unstable")] #[inline] - pub(crate) fn into_owned(self) -> LuaOwnedRef { + pub(crate) fn into_owned(self) -> OwnedValueRef { assert!(self.drop, "Cannot turn non-drop reference into owned"); - let owned_ref = LuaOwnedRef::new(self.lua.clone(), self.index); + let owned_ref = OwnedValueRef::new(self.lua.clone(), self.index); mem::forget(self); owned_ref } } -impl<'lua> fmt::Debug for LuaRef<'lua> { +impl<'lua> fmt::Debug for ValueRef<'lua> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Ref({:p})", self.to_pointer()) } } -impl<'lua> Clone for LuaRef<'lua> { +impl<'lua> Clone for ValueRef<'lua> { fn clone(&self) -> Self { self.lua.clone_ref(self) } } -impl<'lua> Drop for LuaRef<'lua> { +impl<'lua> Drop for ValueRef<'lua> { fn drop(&mut self) { if self.drop { self.lua.drop_ref_index(self.index); @@ -323,7 +323,7 @@ impl<'lua> Drop for LuaRef<'lua> { } } -impl<'lua> PartialEq for LuaRef<'lua> { +impl<'lua> PartialEq for ValueRef<'lua> { fn eq(&self, other: &Self) -> bool { let ref_thread = self.lua.ref_thread(); assert!( @@ -335,28 +335,28 @@ impl<'lua> PartialEq for LuaRef<'lua> { } #[cfg(feature = "unstable")] -pub(crate) struct LuaOwnedRef { +pub(crate) struct OwnedValueRef { pub(crate) inner: Arc, pub(crate) index: c_int, _non_send: PhantomData<*const ()>, } #[cfg(feature = "unstable")] -impl fmt::Debug for LuaOwnedRef { +impl fmt::Debug for OwnedValueRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "OwnedRef({:p})", self.to_ref().to_pointer()) } } #[cfg(feature = "unstable")] -impl Clone for LuaOwnedRef { +impl Clone for OwnedValueRef { fn clone(&self) -> Self { self.to_ref().clone().into_owned() } } #[cfg(feature = "unstable")] -impl Drop for LuaOwnedRef { +impl Drop for OwnedValueRef { fn drop(&mut self) { let lua: &Lua = unsafe { mem::transmute(&self.inner) }; lua.drop_ref_index(self.index); @@ -364,17 +364,17 @@ impl Drop for LuaOwnedRef { } #[cfg(feature = "unstable")] -impl LuaOwnedRef { +impl OwnedValueRef { pub(crate) const fn new(inner: Arc, index: c_int) -> Self { - LuaOwnedRef { + OwnedValueRef { inner, index, _non_send: PhantomData, } } - pub(crate) const fn to_ref(&self) -> LuaRef { - LuaRef { + pub(crate) const fn to_ref(&self) -> ValueRef { + ValueRef { lua: unsafe { mem::transmute(&self.inner) }, index: self.index, drop: false, @@ -531,8 +531,8 @@ mod assertions { use super::*; static_assertions::assert_impl_all!(RegistryKey: Send, Sync); - static_assertions::assert_not_impl_any!(LuaRef: Send); + static_assertions::assert_not_impl_any!(ValueRef: Send); #[cfg(feature = "unstable")] - static_assertions::assert_not_impl_any!(LuaOwnedRef: Send); + static_assertions::assert_not_impl_any!(OwnedValueRef: Send); } diff --git a/src/userdata.rs b/src/userdata.rs index 621910c..d7858e1 100644 --- a/src/userdata.rs +++ b/src/userdata.rs @@ -22,7 +22,7 @@ use crate::function::Function; use crate::lua::Lua; use crate::string::String; use crate::table::{Table, TablePairs}; -use crate::types::{LuaRef, MaybeSend, SubtypeId}; +use crate::types::{MaybeSend, SubtypeId, ValueRef}; use crate::util::{check_stack, get_userdata, take_userdata, StackGuard}; use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, Value}; use crate::UserDataRegistry; @@ -791,7 +791,7 @@ impl Deref for UserDataVariant { /// [`is`]: crate::AnyUserData::is /// [`borrow`]: crate::AnyUserData::borrow #[derive(Clone, Debug)] -pub struct AnyUserData<'lua>(pub(crate) LuaRef<'lua>, pub(crate) SubtypeId); +pub struct AnyUserData<'lua>(pub(crate) ValueRef<'lua>, pub(crate) SubtypeId); /// Owned handle to an internal Lua userdata. /// @@ -801,7 +801,7 @@ pub struct AnyUserData<'lua>(pub(crate) LuaRef<'lua>, pub(crate) SubtypeId); #[cfg(feature = "unstable")] #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))] #[derive(Clone, Debug)] -pub struct OwnedAnyUserData(pub(crate) crate::types::LuaOwnedRef, pub(crate) SubtypeId); +pub struct OwnedAnyUserData(pub(crate) crate::types::OwnedValueRef, pub(crate) SubtypeId); #[cfg(feature = "unstable")] impl OwnedAnyUserData {