Add RawLua::create_table_from (internal)

This commit is contained in:
Alex Orlenko
2025-06-11 23:55:06 +01:00
parent caeac2e9a3
commit a2dc662a92
2 changed files with 30 additions and 25 deletions
+2 -25
View File
@@ -24,9 +24,7 @@ use crate::types::{
ReentrantMutexGuard, RegistryKey, VmState, XRc, XWeak,
};
use crate::userdata::{AnyUserData, UserData, UserDataProxy, UserDataRegistry, UserDataStorage};
use crate::util::{
assert_stack, check_stack, protect_lua_closure, push_string, push_table, rawset_field, StackGuard,
};
use crate::util::{assert_stack, check_stack, protect_lua_closure, push_string, rawset_field, StackGuard};
use crate::value::{Nil, Value};
#[cfg(not(feature = "luau"))]
@@ -1202,28 +1200,7 @@ impl Lua {
K: IntoLua,
V: IntoLua,
{
let lua = self.lock();
let state = lua.state();
unsafe {
let _sg = StackGuard::new(state);
check_stack(state, 6)?;
let iter = iter.into_iter();
let lower_bound = iter.size_hint().0;
let protect = !lua.unlikely_memory_error();
push_table(state, 0, lower_bound, protect)?;
for (k, v) in iter {
lua.push(k)?;
lua.push(v)?;
if protect {
protect_lua!(state, 3, 1, fn(state) ffi::lua_rawset(state, -3))?;
} else {
ffi::lua_rawset(state, -3);
}
}
Ok(Table(lua.pop_ref()))
}
unsafe { self.lock().create_table_from(iter) }
}
/// Creates a table from an iterator of values, using `1..` as the keys.
+28
View File
@@ -537,6 +537,34 @@ impl RawLua {
Ok(Table(self.pop_ref()))
}
/// See [`Lua::create_table_from`]
pub(crate) unsafe fn create_table_from<I, K, V>(&self, iter: I) -> Result<Table>
where
I: IntoIterator<Item = (K, V)>,
K: IntoLua,
V: IntoLua,
{
let state = self.state();
let _sg = StackGuard::new(state);
check_stack(state, 6)?;
let iter = iter.into_iter();
let lower_bound = iter.size_hint().0;
let protect = !self.unlikely_memory_error();
push_table(state, 0, lower_bound, protect)?;
for (k, v) in iter {
self.push(k)?;
self.push(v)?;
if protect {
protect_lua!(state, 3, 1, fn(state) ffi::lua_rawset(state, -3))?;
} else {
ffi::lua_rawset(state, -3);
}
}
Ok(Table(self.pop_ref()))
}
/// See [`Lua::create_sequence_from`]
pub(crate) unsafe fn create_sequence_from<T, I>(&self, iter: I) -> Result<Table>
where