diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2022-10-16 23:36:26 +0100 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2022-12-18 00:35:41 +0000 |
commit | bf79d9e75dabaed9278084f49fb0a162002eb8a8 (patch) | |
tree | 6e1f24d1a3b7021a4bcef75b40ce7483e3d8ce48 /src/types.rs | |
parent | 02c08c6350092f7c94d40251e649e0e1116f2374 (diff) | |
download | mlua-bf79d9e75dabaed9278084f49fb0a162002eb8a8.zip |
Initial implementation of owned Lua types
Diffstat (limited to 'src/types.rs')
-rw-r--r-- | src/types.rs | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/src/types.rs b/src/types.rs index 9404d10..d5a5413 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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, + } + } +} |