mirror of
https://github.com/mlua-rs/mlua
synced 2026-06-08 16:05:43 +00:00
Initial implementation of owned Lua types
This commit is contained in:
+53
-1
@@ -180,6 +180,21 @@ impl RegistryKey {
|
||||
pub(crate) struct LuaRef<'lua> {
|
||||
pub(crate) lua: &'lua Lua,
|
||||
pub(crate) index: c_int,
|
||||
pub(crate) drop: bool,
|
||||
}
|
||||
|
||||
impl<'lua> LuaRef<'lua> {
|
||||
pub(crate) const fn new(lua: &'lua Lua, index: c_int) -> Self {
|
||||
LuaRef {
|
||||
lua,
|
||||
index,
|
||||
drop: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn into_owned(self) -> LuaOwnedRef {
|
||||
self.lua.make_owned_ref(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'lua> fmt::Debug for LuaRef<'lua> {
|
||||
@@ -196,7 +211,7 @@ impl<'lua> Clone for LuaRef<'lua> {
|
||||
|
||||
impl<'lua> Drop for LuaRef<'lua> {
|
||||
fn drop(&mut self) {
|
||||
if self.index > 0 {
|
||||
if self.drop {
|
||||
self.lua.drop_ref(self);
|
||||
}
|
||||
}
|
||||
@@ -214,3 +229,40 @@ impl<'lua> PartialEq for LuaRef<'lua> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct LuaOwnedRef {
|
||||
pub(crate) lua: Lua,
|
||||
pub(crate) index: c_int,
|
||||
}
|
||||
|
||||
impl fmt::Debug for LuaOwnedRef {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "OwnedRef({})", self.index)
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for LuaOwnedRef {
|
||||
fn clone(&self) -> Self {
|
||||
self.lua.make_owned_ref(self.to_ref().clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for LuaOwnedRef {
|
||||
fn drop(&mut self) {
|
||||
self.lua.drop_ref(&LuaRef {
|
||||
lua: &self.lua,
|
||||
index: self.index,
|
||||
drop: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl LuaOwnedRef {
|
||||
pub(crate) const fn to_ref(&self) -> LuaRef {
|
||||
LuaRef {
|
||||
lua: &self.lua,
|
||||
index: self.index,
|
||||
drop: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user