Skip to content

Commit a2dc662

Browse files
committed
Add RawLua::create_table_from (internal)
1 parent caeac2e commit a2dc662

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

src/state.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ use crate::types::{
2424
ReentrantMutexGuard, RegistryKey, VmState, XRc, XWeak,
2525
};
2626
use crate::userdata::{AnyUserData, UserData, UserDataProxy, UserDataRegistry, UserDataStorage};
27-
use crate::util::{
28-
assert_stack, check_stack, protect_lua_closure, push_string, push_table, rawset_field, StackGuard,
29-
};
27+
use crate::util::{assert_stack, check_stack, protect_lua_closure, push_string, rawset_field, StackGuard};
3028
use crate::value::{Nil, Value};
3129

3230
#[cfg(not(feature = "luau"))]
@@ -1202,28 +1200,7 @@ impl Lua {
12021200
K: IntoLua,
12031201
V: IntoLua,
12041202
{
1205-
let lua = self.lock();
1206-
let state = lua.state();
1207-
unsafe {
1208-
let _sg = StackGuard::new(state);
1209-
check_stack(state, 6)?;
1210-
1211-
let iter = iter.into_iter();
1212-
let lower_bound = iter.size_hint().0;
1213-
let protect = !lua.unlikely_memory_error();
1214-
push_table(state, 0, lower_bound, protect)?;
1215-
for (k, v) in iter {
1216-
lua.push(k)?;
1217-
lua.push(v)?;
1218-
if protect {
1219-
protect_lua!(state, 3, 1, fn(state) ffi::lua_rawset(state, -3))?;
1220-
} else {
1221-
ffi::lua_rawset(state, -3);
1222-
}
1223-
}
1224-
1225-
Ok(Table(lua.pop_ref()))
1226-
}
1203+
unsafe { self.lock().create_table_from(iter) }
12271204
}
12281205

12291206
/// Creates a table from an iterator of values, using `1..` as the keys.

src/state/raw.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,34 @@ impl RawLua {
537537
Ok(Table(self.pop_ref()))
538538
}
539539

540+
/// See [`Lua::create_table_from`]
541+
pub(crate) unsafe fn create_table_from<I, K, V>(&self, iter: I) -> Result<Table>
542+
where
543+
I: IntoIterator<Item = (K, V)>,
544+
K: IntoLua,
545+
V: IntoLua,
546+
{
547+
let state = self.state();
548+
let _sg = StackGuard::new(state);
549+
check_stack(state, 6)?;
550+
551+
let iter = iter.into_iter();
552+
let lower_bound = iter.size_hint().0;
553+
let protect = !self.unlikely_memory_error();
554+
push_table(state, 0, lower_bound, protect)?;
555+
for (k, v) in iter {
556+
self.push(k)?;
557+
self.push(v)?;
558+
if protect {
559+
protect_lua!(state, 3, 1, fn(state) ffi::lua_rawset(state, -3))?;
560+
} else {
561+
ffi::lua_rawset(state, -3);
562+
}
563+
}
564+
565+
Ok(Table(self.pop_ref()))
566+
}
567+
540568
/// See [`Lua::create_sequence_from`]
541569
pub(crate) unsafe fn create_sequence_from<T, I>(&self, iter: I) -> Result<Table>
542570
where

0 commit comments

Comments
 (0)