Use stack_guard when the function doesn't return Result

Also, simplify some things that used to use error_guard but now are not
This commit is contained in:
kyren 2017-07-25 00:37:44 -04:00
parent 3579b83888
commit 8194c4d411
1 changed files with 13 additions and 15 deletions

View File

@ -213,7 +213,7 @@ impl<'lua> String<'lua> {
pub fn as_bytes(&self) -> &[u8] { pub fn as_bytes(&self) -> &[u8] {
let lua = self.0.lua; let lua = self.0.lua;
unsafe { unsafe {
stack_err_guard(lua.state, 0, || { stack_guard(lua.state, 0, || {
check_stack(lua.state, 1); check_stack(lua.state, 1);
lua.push_ref(lua.state, &self.0); lua.push_ref(lua.state, &self.0);
assert_eq!(ffi::lua_type(lua.state, -1), ffi::LUA_TSTRING); assert_eq!(ffi::lua_type(lua.state, -1), ffi::LUA_TSTRING);
@ -222,8 +222,8 @@ impl<'lua> String<'lua> {
let data = ffi::lua_tolstring(lua.state, -1, &mut size); let data = ffi::lua_tolstring(lua.state, -1, &mut size);
ffi::lua_pop(lua.state, 1); ffi::lua_pop(lua.state, 1);
Ok(slice::from_raw_parts(data as *const u8, size)) slice::from_raw_parts(data as *const u8, size)
}).expect("infallible stack_err_guard failed") // this conversion cannot fail })
} }
} }
} }
@ -241,16 +241,16 @@ impl<'lua> Table<'lua> {
/// desired. /// desired.
pub fn set<K: ToLua<'lua>, V: ToLua<'lua>>(&self, key: K, value: V) -> Result<()> { pub fn set<K: ToLua<'lua>, V: ToLua<'lua>>(&self, key: K, value: V) -> Result<()> {
let lua = self.0.lua; let lua = self.0.lua;
let key = key.to_lua(lua)?;
let value = value.to_lua(lua)?;
unsafe { unsafe {
stack_err_guard(lua.state, 0, || {
check_stack(lua.state, 7); check_stack(lua.state, 7);
lua.push_ref(lua.state, &self.0); lua.push_ref(lua.state, &self.0);
lua.push_value(lua.state, key); lua.push_value(lua.state, key.to_lua(lua)?);
lua.push_value(lua.state, value); lua.push_value(lua.state, value.to_lua(lua)?);
psettable(lua.state, -3)?; psettable(lua.state, -3)?;
ffi::lua_pop(lua.state, 1); ffi::lua_pop(lua.state, 1);
Ok(()) Ok(())
})
} }
} }
@ -261,7 +261,6 @@ impl<'lua> Table<'lua> {
/// This might invoke the `__index` metamethod. Use the `raw_get` method if that is not desired. /// This might invoke the `__index` metamethod. Use the `raw_get` method if that is not desired.
pub fn get<K: ToLua<'lua>, V: FromLua<'lua>>(&self, key: K) -> Result<V> { pub fn get<K: ToLua<'lua>, V: FromLua<'lua>>(&self, key: K) -> Result<V> {
let lua = self.0.lua; let lua = self.0.lua;
let key = key.to_lua(lua)?;
unsafe { unsafe {
stack_err_guard(lua.state, 0, || { stack_err_guard(lua.state, 0, || {
check_stack(lua.state, 5); check_stack(lua.state, 5);
@ -278,12 +277,11 @@ impl<'lua> Table<'lua> {
/// Checks whether the table contains a non-nil value for `key`. /// Checks whether the table contains a non-nil value for `key`.
pub fn contains_key<K: ToLua<'lua>>(&self, key: K) -> Result<bool> { pub fn contains_key<K: ToLua<'lua>>(&self, key: K) -> Result<bool> {
let lua = self.0.lua; let lua = self.0.lua;
let key = key.to_lua(lua)?;
unsafe { unsafe {
stack_err_guard(lua.state, 0, || { stack_err_guard(lua.state, 0, || {
check_stack(lua.state, 5); check_stack(lua.state, 5);
lua.push_ref(lua.state, &self.0); lua.push_ref(lua.state, &self.0);
lua.push_value(lua.state, key); lua.push_value(lua.state, key.to_lua(lua)?);
pgettable(lua.state, -2)?; pgettable(lua.state, -2)?;
let has = ffi::lua_isnil(lua.state, -1) == 0; let has = ffi::lua_isnil(lua.state, -1) == 0;
ffi::lua_pop(lua.state, 2); ffi::lua_pop(lua.state, 2);