Initial implementation of owned Lua types

This commit is contained in:
Alex Orlenko
2022-10-16 23:36:26 +01:00
parent 02c08c6350
commit bf79d9e75d
10 changed files with 267 additions and 39 deletions
+18 -1
View File
@@ -11,7 +11,7 @@ use {
use crate::error::{Error, Result};
use crate::ffi;
use crate::function::Function;
use crate::types::{Integer, LuaRef};
use crate::types::{Integer, LuaOwnedRef, LuaRef};
use crate::util::{assert_stack, check_stack, StackGuard};
use crate::value::{FromLua, FromLuaMulti, Nil, ToLua, ToLuaMulti, Value};
@@ -22,6 +22,17 @@ use {futures_core::future::LocalBoxFuture, futures_util::future};
#[derive(Clone, Debug)]
pub struct Table<'lua>(pub(crate) LuaRef<'lua>);
/// Owned handle to an internal Lua table.
#[derive(Clone, Debug)]
pub struct OwnedTable(pub(crate) LuaOwnedRef);
impl OwnedTable {
/// Get borrowed handle to the underlying Lua table.
pub const fn to_ref(&self) -> Table {
Table(self.0.to_ref())
}
}
#[allow(clippy::len_without_is_empty)]
impl<'lua> Table<'lua> {
/// Sets a key-value pair in the table.
@@ -512,6 +523,12 @@ impl<'lua> Table<'lua> {
unsafe { ffi::lua_topointer(ref_thread, self.0.index) }
}
/// Convert this handle to owned version.
#[inline]
pub fn into_owned(self) -> OwnedTable {
OwnedTable(self.0.into_owned())
}
/// Consume this table and return an iterator over the pairs of the table.
///
/// This works like the Lua `pairs` function, but does not invoke the `__pairs` metamethod.