From 773bf3e9ba38ccd2ecae595720a79a249dcc04de Mon Sep 17 00:00:00 2001 From: kyren Date: Tue, 24 Oct 2017 16:15:57 -0400 Subject: [PATCH] Fix some clippy lints, possible edge case API incompatibility around HashMap --- src/conversion.rs | 8 ++-- src/ffi.rs | 2 +- src/lua.rs | 3 +- src/util.rs | 107 ++++++++++++++++++++++------------------------ 4 files changed, 59 insertions(+), 61 deletions(-) diff --git a/src/conversion.rs b/src/conversion.rs index 306249b..be19414 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -1,5 +1,5 @@ use std::collections::{BTreeMap, HashMap}; -use std::hash::Hash; +use std::hash::{BuildHasher, Hash}; use std::string::String as StdString; use error::*; @@ -266,13 +266,15 @@ impl<'lua, T: FromLua<'lua>> FromLua<'lua> for Vec { } } -impl<'lua, K: Eq + Hash + ToLua<'lua>, V: ToLua<'lua>> ToLua<'lua> for HashMap { +impl<'lua, K: Eq + Hash + ToLua<'lua>, V: ToLua<'lua>, S: BuildHasher> ToLua<'lua> + for HashMap { fn to_lua(self, lua: &'lua Lua) -> Result> { Ok(Value::Table(lua.create_table_from(self)?)) } } -impl<'lua, K: Eq + Hash + FromLua<'lua>, V: FromLua<'lua>> FromLua<'lua> for HashMap { +impl<'lua, K: Eq + Hash + FromLua<'lua>, V: FromLua<'lua>, S: BuildHasher + Default> FromLua<'lua> + for HashMap { fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result { if let Value::Table(table) = value { table.pairs().collect() diff --git a/src/ffi.rs b/src/ffi.rs index 2d12311..05ab3fa 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -36,7 +36,7 @@ pub const LUA_NOREF: c_int = -2; pub const LUA_REFNIL: c_int = -1; pub const LUA_MULTRET: c_int = -1; -pub const LUAI_MAXSTACK: c_int = 1000000; +pub const LUAI_MAXSTACK: c_int = 1_000_000; pub const LUA_REGISTRYINDEX: c_int = -LUAI_MAXSTACK - 1000; pub const LUA_RIDX_MAINTHREAD: lua_Integer = 1; pub const LUA_RIDX_GLOBALS: lua_Integer = 2; diff --git a/src/lua.rs b/src/lua.rs index ac7c4f5..b765dfd 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -64,8 +64,7 @@ impl<'lua> Value<'lua> { Value::Table(_) => "table", Value::Function(_) => "function", Value::Thread(_) => "thread", - Value::UserData(_) => "userdata", - Value::Error(_) => "userdata", + Value::UserData(_) | Value::Error(_) => "userdata", } } } diff --git a/src/util.rs b/src/util.rs index 9ee5b0d..bf045e6 100644 --- a/src/util.rs +++ b/src/util.rs @@ -260,62 +260,59 @@ pub unsafe fn pnext(state: *mut ffi::lua_State, index: c_int) -> Result { pub unsafe fn handle_error(state: *mut ffi::lua_State, err: c_int) -> Result<()> { if err == ffi::LUA_OK || err == ffi::LUA_YIELD { Ok(()) - } else { - if let Some(err) = pop_wrapped_error(state) { - Err(err) - } else if is_wrapped_panic(state, -1) { - let panic = get_userdata::(state, -1); - if let Some(p) = (*panic).0.take() { - ffi::lua_settop(state, 0); - resume_unwind(p); - } else { - lua_panic!(state, "internal error: panic was resumed twice") - } + } else if let Some(err) = pop_wrapped_error(state) { + Err(err) + } else if is_wrapped_panic(state, -1) { + let panic = get_userdata::(state, -1); + if let Some(p) = (*panic).0.take() { + ffi::lua_settop(state, 0); + resume_unwind(p); } else { - let err_string = - if let Some(s) = ffi::lua_tolstring(state, -1, ptr::null_mut()).as_ref() { - CStr::from_ptr(s) - .to_str() - .unwrap_or_else(|_| "") - .to_owned() - } else { - "".to_owned() - }; - ffi::lua_pop(state, 1); - - Err(match err { - ffi::LUA_ERRRUN => Error::RuntimeError(err_string), - ffi::LUA_ERRSYNTAX => { - Error::SyntaxError { - // This seems terrible, but as far as I can tell, this is exactly what the - // stock Lua REPL does. - incomplete_input: err_string.ends_with(""), - message: err_string, - } - } - ffi::LUA_ERRERR => { - // The Lua manual documents this error wrongly: It is not raised when a message - // handler errors, but rather when some specific situations regarding stack - // overflow handling occurs. Since it is not very useful do differentiate - // between that and "ordinary" runtime errors, we handle them the same way. - Error::RuntimeError(err_string) - } - ffi::LUA_ERRMEM => { - // This should be impossible, as we set the lua allocator to one that aborts - // instead of failing. - eprintln!("Lua memory error, aborting!"); - process::abort() - } - ffi::LUA_ERRGCMM => { - // This should be impossible, since we wrap setmetatable to protect __gc - // metamethods, but if we do end up here then the same logic as setmetatable - // applies and we must abort. - eprintln!("Lua error during __gc, aborting!"); - process::abort() - } - _ => lua_panic!(state, "internal error: unrecognized lua error code"), - }) + lua_panic!(state, "internal error: panic was resumed twice") } + } else { + let err_string = if let Some(s) = ffi::lua_tolstring(state, -1, ptr::null_mut()).as_ref() { + CStr::from_ptr(s) + .to_str() + .unwrap_or_else(|_| "") + .to_owned() + } else { + "".to_owned() + }; + ffi::lua_pop(state, 1); + + Err(match err { + ffi::LUA_ERRRUN => Error::RuntimeError(err_string), + ffi::LUA_ERRSYNTAX => { + Error::SyntaxError { + // This seems terrible, but as far as I can tell, this is exactly what the + // stock Lua REPL does. + incomplete_input: err_string.ends_with(""), + message: err_string, + } + } + ffi::LUA_ERRERR => { + // The Lua manual documents this error wrongly: It is not raised when a message + // handler errors, but rather when some specific situations regarding stack + // overflow handling occurs. Since it is not very useful do differentiate + // between that and "ordinary" runtime errors, we handle them the same way. + Error::RuntimeError(err_string) + } + ffi::LUA_ERRMEM => { + // This should be impossible, as we set the lua allocator to one that aborts + // instead of failing. + eprintln!("Lua memory error, aborting!"); + process::abort() + } + ffi::LUA_ERRGCMM => { + // This should be impossible, since we wrap setmetatable to protect __gc + // metamethods, but if we do end up here then the same logic as setmetatable + // applies and we must abort. + eprintln!("Lua error during __gc, aborting!"); + process::abort() + } + _ => lua_panic!(state, "internal error: unrecognized lua error code"), + }) } } @@ -695,7 +692,7 @@ pub unsafe fn is_wrapped_panic(state: *mut ffi::lua_State, index: c_int) -> bool get_panic_metatable(state); let res = ffi::lua_rawequal(state, -1, -2) != 0; ffi::lua_pop(state, 2); - return res; + res } pub unsafe fn get_error_metatable(state: *mut ffi::lua_State) -> c_int {