From 8f5b0335c16afdc2cdc205c6c98838cf6c74997e Mon Sep 17 00:00:00 2001 From: Terence Date: Thu, 15 Feb 2018 13:01:48 -0500 Subject: [PATCH 1/3] Implement LuaRead for Vec of any readable type, not just AnyLuaValue. --- hlua/src/rust_tables.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) mode change 100644 => 100755 hlua/src/rust_tables.rs diff --git a/hlua/src/rust_tables.rs b/hlua/src/rust_tables.rs old mode 100644 new mode 100755 index 77126f80..f5c542d0 --- a/hlua/src/rust_tables.rs +++ b/hlua/src/rust_tables.rs @@ -101,14 +101,15 @@ impl<'lua, L, T, E> PushOne for Vec { } -impl<'lua, L> LuaRead for Vec - where L: AsMutLua<'lua> +impl<'lua, L, V> LuaRead for Vec + where L: AsMutLua<'lua>, + V: for<'a> LuaRead<&'a mut L> { fn lua_read_at_position(lua: L, index: i32) -> Result { // We need this as iteration order isn't guaranteed to match order of // keys, even if they're numeric // https://www.lua.org/manual/5.2/manual.html#pdf-next - let mut dict: BTreeMap = BTreeMap::new(); + let mut dict: BTreeMap = BTreeMap::new(); let mut me = lua; unsafe { ffi::lua_pushnil(me.as_mut_lua().0) }; @@ -132,7 +133,7 @@ impl<'lua, L> LuaRead for Vec } }; - let value: AnyLuaValue = + let value: V = LuaRead::lua_read_at_position(&mut me, -1).ok().unwrap(); unsafe { ffi::lua_pop(me.as_mut_lua().0, 1) }; From bd3cafa4a79371ac18d03319907369d5c3a2710f Mon Sep 17 00:00:00 2001 From: Terence Date: Thu, 15 Mar 2018 17:43:44 -0400 Subject: [PATCH 2/3] don't panic on failing to LuaRead a Vec --- hlua/src/rust_tables.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/hlua/src/rust_tables.rs b/hlua/src/rust_tables.rs index f5c542d0..05323bc7 100755 --- a/hlua/src/rust_tables.rs +++ b/hlua/src/rust_tables.rs @@ -133,8 +133,18 @@ impl<'lua, L, V> LuaRead for Vec } }; - let value: V = - LuaRead::lua_read_at_position(&mut me, -1).ok().unwrap(); + let value = { + let maybe_value: Option = + LuaRead::lua_read_at_position(&mut me, -1).ok(); + match maybe_value { + None => { + // Cleaning up after ourselves + unsafe { ffi::lua_pop(me.as_mut_lua().0, 2) }; + return Err(me) + } + Some(v) => v, + } + }; unsafe { ffi::lua_pop(me.as_mut_lua().0, 1) }; From ead5cf3eb873d80690b077a7f8afb32d6e12ea11 Mon Sep 17 00:00:00 2001 From: Terence Date: Thu, 15 Mar 2018 18:30:40 -0400 Subject: [PATCH 3/3] Add type annotations to table tests --- hlua/src/rust_tables.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/hlua/src/rust_tables.rs b/hlua/src/rust_tables.rs index 05323bc7..fb51f560 100755 --- a/hlua/src/rust_tables.rs +++ b/hlua/src/rust_tables.rs @@ -371,13 +371,9 @@ mod tests { lua.set("v", &orig[..]); - let read: Vec<_> = lua.get("v").unwrap(); + let read: Vec = lua.get("v").unwrap(); for (o, r) in orig.iter().zip(read.iter()) { - if let AnyLuaValue::LuaNumber(ref n) = *r { - assert_eq!(o, n); - } else { - panic!("Unexpected variant"); - } + assert_eq!(*o, *r); } } @@ -387,7 +383,7 @@ mod tests { lua.execute::<()>(r#"v = { [-1] = -1, [2] = 2, [42] = 42 }"#).unwrap(); - let read: Option> = lua.get("v"); + let read: Option> = lua.get("v"); if read.is_some() { panic!("Unexpected success"); } @@ -399,7 +395,7 @@ mod tests { lua.execute::<()>(r#"v = { }"#).unwrap(); - let read: Vec<_> = lua.get("v").unwrap(); + let read: Vec = lua.get("v").unwrap(); assert_eq!(read.len(), 0); } @@ -409,7 +405,7 @@ mod tests { lua.execute::<()>(r#"v = { [-1] = -1, ["foo"] = 2, [{}] = 42 }"#).unwrap(); - let read: Option> = lua.get("v"); + let read: Option> = lua.get("v"); if read.is_some() { panic!("Unexpected success"); } @@ -429,7 +425,7 @@ mod tests { lua.set("v", &orig[..]); - let read: Vec<_> = lua.get("v").unwrap(); + let read: Vec = lua.get("v").unwrap(); assert_eq!(read, orig); } @@ -439,11 +435,11 @@ mod tests { lua.execute::<()>(r#"v = { 1, 2, 3 }"#).unwrap(); - let read: Vec<_> = lua.get("v").unwrap(); + let read: Vec = lua.get("v").unwrap(); assert_eq!( read, [1., 2., 3.].iter() - .map(|x| AnyLuaValue::LuaNumber(*x)).collect::>()); + .map(|x| AnyLuaValue::LuaNumber(*x)).collect::>()); } #[test]