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,8 +260,7 @@ 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);
@ -272,8 +271,7 @@ pub unsafe fn handle_error(state: *mut ffi::lua_State, err: c_int) -> Result<()>
lua_panic!(state, "internal error: panic was resumed twice") lua_panic!(state, "internal error: panic was resumed twice")
} }
} else { } else {
let err_string = let err_string = if let Some(s) = ffi::lua_tolstring(state, -1, ptr::null_mut()).as_ref() {
if let Some(s) = ffi::lua_tolstring(state, -1, ptr::null_mut()).as_ref() {
CStr::from_ptr(s) CStr::from_ptr(s)
.to_str() .to_str()
.unwrap_or_else(|_| "<unprintable error>") .unwrap_or_else(|_| "<unprintable error>")
@ -316,7 +314,6 @@ pub unsafe fn handle_error(state: *mut ffi::lua_State, err: c_int) -> Result<()>
_ => lua_panic!(state, "internal error: unrecognized lua error code"), _ => lua_panic!(state, "internal error: unrecognized lua error code"),
}) })
} }
}
} }
pub unsafe fn push_string(state: *mut ffi::lua_State, s: &str) { pub unsafe fn push_string(state: *mut ffi::lua_State, s: &str) {
@ -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 {