Fix some clippy lints, possible edge case API incompatibility around HashMap

This commit is contained in:
kyren 2017-10-24 16:15:57 -04:00
parent 4bbeeb2b2d
commit 773bf3e9ba
4 changed files with 59 additions and 61 deletions

View File

@ -1,5 +1,5 @@
use std::collections::{BTreeMap, HashMap}; use std::collections::{BTreeMap, HashMap};
use std::hash::Hash; use std::hash::{BuildHasher, Hash};
use std::string::String as StdString; use std::string::String as StdString;
use error::*; use error::*;
@ -266,13 +266,15 @@ impl<'lua, T: FromLua<'lua>> FromLua<'lua> for Vec<T> {
} }
} }
impl<'lua, K: Eq + Hash + ToLua<'lua>, V: ToLua<'lua>> ToLua<'lua> for HashMap<K, V> { impl<'lua, K: Eq + Hash + ToLua<'lua>, V: ToLua<'lua>, S: BuildHasher> ToLua<'lua>
for HashMap<K, V, S> {
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> { fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::Table(lua.create_table_from(self)?)) Ok(Value::Table(lua.create_table_from(self)?))
} }
} }
impl<'lua, K: Eq + Hash + FromLua<'lua>, V: FromLua<'lua>> FromLua<'lua> for HashMap<K, V> { impl<'lua, K: Eq + Hash + FromLua<'lua>, V: FromLua<'lua>, S: BuildHasher + Default> FromLua<'lua>
for HashMap<K, V, S> {
fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result<Self> { fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result<Self> {
if let Value::Table(table) = value { if let Value::Table(table) = value {
table.pairs().collect() table.pairs().collect()

View File

@ -36,7 +36,7 @@ pub const LUA_NOREF: c_int = -2;
pub const LUA_REFNIL: c_int = -1; pub const LUA_REFNIL: c_int = -1;
pub const LUA_MULTRET: 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_REGISTRYINDEX: c_int = -LUAI_MAXSTACK - 1000;
pub const LUA_RIDX_MAINTHREAD: lua_Integer = 1; pub const LUA_RIDX_MAINTHREAD: lua_Integer = 1;
pub const LUA_RIDX_GLOBALS: lua_Integer = 2; pub const LUA_RIDX_GLOBALS: lua_Integer = 2;

View File

@ -64,8 +64,7 @@ impl<'lua> Value<'lua> {
Value::Table(_) => "table", Value::Table(_) => "table",
Value::Function(_) => "function", Value::Function(_) => "function",
Value::Thread(_) => "thread", Value::Thread(_) => "thread",
Value::UserData(_) => "userdata", Value::UserData(_) | Value::Error(_) => "userdata",
Value::Error(_) => "userdata",
} }
} }
} }

View File

@ -260,62 +260,59 @@ pub unsafe fn pnext(state: *mut ffi::lua_State, index: c_int) -> Result<c_int> {
pub unsafe fn handle_error(state: *mut ffi::lua_State, err: 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 { if err == ffi::LUA_OK || err == ffi::LUA_YIELD {
Ok(()) Ok(())
} else { } else if let Some(err) = pop_wrapped_error(state) {
if let Some(err) = pop_wrapped_error(state) { Err(err)
Err(err) } else if is_wrapped_panic(state, -1) {
} else if is_wrapped_panic(state, -1) { let panic = get_userdata::<WrappedPanic>(state, -1);
let panic = get_userdata::<WrappedPanic>(state, -1); if let Some(p) = (*panic).0.take() {
if let Some(p) = (*panic).0.take() { ffi::lua_settop(state, 0);
ffi::lua_settop(state, 0); resume_unwind(p);
resume_unwind(p);
} else {
lua_panic!(state, "internal error: panic was resumed twice")
}
} else { } else {
let err_string = lua_panic!(state, "internal error: panic was resumed twice")
if let Some(s) = ffi::lua_tolstring(state, -1, ptr::null_mut()).as_ref() {
CStr::from_ptr(s)
.to_str()
.unwrap_or_else(|_| "<unprintable error>")
.to_owned()
} else {
"<unprintable error>".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("<eof>"),
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"),
})
} }
} 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(|_| "<unprintable error>")
.to_owned()
} else {
"<unprintable error>".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("<eof>"),
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); get_panic_metatable(state);
let res = ffi::lua_rawequal(state, -1, -2) != 0; let res = ffi::lua_rawequal(state, -1, -2) != 0;
ffi::lua_pop(state, 2); ffi::lua_pop(state, 2);
return res; res
} }
pub unsafe fn get_error_metatable(state: *mut ffi::lua_State) -> c_int { pub unsafe fn get_error_metatable(state: *mut ffi::lua_State) -> c_int {