Skip to content

Commit 9126bb8

Browse files
committed
Add RawLua::pop method
1 parent 7f1d716 commit 9126bb8

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/state/raw.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::stdlib::StdLib;
1616
use crate::string::LuaString;
1717
use crate::table::Table;
1818
use crate::thread::Thread;
19-
use crate::traits::IntoLua;
19+
use crate::traits::{FromLua, IntoLua};
2020
use crate::types::{
2121
AppDataRef, AppDataRefMut, Callback, CallbackUpvalue, DestructedUserdata, Integer, LightUserData,
2222
LuaType, MaybeSend, ReentrantMutex, RegistryKey, ValueRef, XRc,
@@ -50,7 +50,7 @@ use {
5050
std::task::{Context, Poll, Waker},
5151
};
5252

53-
/// An inner Lua struct which holds a raw Lua state.
53+
/// An internal Lua struct which holds a raw Lua state.
5454
#[doc(hidden)]
5555
pub struct RawLua {
5656
// The state is dynamic and depends on context
@@ -736,9 +736,19 @@ impl RawLua {
736736
value.push_into_stack(self)
737737
}
738738

739+
/// Pops a value that implements [`FromLua`] from the top of the Lua stack.
740+
///
741+
/// Uses up to 1 stack space, does not call `checkstack`.
742+
#[inline(always)]
743+
pub unsafe fn pop<R: FromLua>(&self) -> Result<R> {
744+
let v = R::from_stack(-1, self)?;
745+
ffi::lua_pop(self.state(), 1);
746+
Ok(v)
747+
}
748+
739749
/// Pushes a `Value` (by reference) onto the Lua stack.
740750
///
741-
/// Uses 2 stack spaces, does not call `checkstack`.
751+
/// Uses up to 2 stack spaces, does not call `checkstack`.
742752
pub unsafe fn push_value(&self, value: &Value) -> Result<()> {
743753
let state = self.state();
744754
match value {

src/table.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -784,10 +784,8 @@ impl Table {
784784
ffi::lua_pushnil(state);
785785
while ffi::lua_next(state, -2) != 0 {
786786
let k = K::from_stack(-2, &lua)?;
787-
let v = V::from_stack(-1, &lua)?;
787+
let v = lua.pop::<V>()?;
788788
f(k, v)?;
789-
// Keep key for next iteration
790-
ffi::lua_pop(state, 1);
791789
}
792790
}
793791
Ok(())
@@ -860,8 +858,7 @@ impl Table {
860858
if len.is_none() && t == ffi::LUA_TNIL {
861859
break;
862860
}
863-
f(V::from_stack(-1, &lua)?)?;
864-
ffi::lua_pop(state, 1);
861+
f(lua.pop::<V>()?)?;
865862
}
866863
}
867864
Ok(())

0 commit comments

Comments
 (0)