From de0f3dc3c0cb8eb7bfb077be8e3706f0f4e9b74a Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sun, 18 Jun 2017 14:31:38 +0200 Subject: [PATCH 1/4] Rename `Lua::load` to `Lua::exec` When talking about "loading" Lua code, it usually means compiling a chunk of code into a runnable Lua function, but without actually running it. This makes that clear. --- examples/examples.rs | 2 +- src/lua.rs | 2 +- src/tests.rs | 32 ++++++++++++++++---------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/examples/examples.rs b/examples/examples.rs index 28f5dbf..df3abe9 100644 --- a/examples/examples.rs +++ b/examples/examples.rs @@ -29,7 +29,7 @@ fn examples() -> LuaResult<()> { // You can load and evaluate lua code. The second parameter here gives the // chunk a better name when lua error messages are printed. - lua.load::<()>( + lua.exec::<()>( r#" global = 'foo'..'bar' "#, diff --git a/src/lua.rs b/src/lua.rs index 45edc88..15442ff 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -905,7 +905,7 @@ impl Lua { /// results in better error traces. /// /// Returns the values returned by the chunk. - pub fn load<'lua, R: FromLuaMulti<'lua>>( + pub fn exec<'lua, R: FromLuaMulti<'lua>>( &'lua self, source: &str, name: Option<&str>, diff --git a/src/tests.rs b/src/tests.rs index 265668a..801db5e 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -20,7 +20,7 @@ fn test_set_get() { fn test_load() { let lua = Lua::new(); let globals = lua.globals().unwrap(); - lua.load::<()>( + lua.exec::<()>( r#" res = 'foo'..'bar' "#, @@ -28,7 +28,7 @@ fn test_load() { ).unwrap(); assert_eq!(globals.get::<_, String>("res").unwrap(), "foobar"); - let module: LuaTable = lua.load( + let module: LuaTable = lua.exec( r#" local module = {} @@ -80,7 +80,7 @@ fn test_table() { assert_eq!(table2.get::<_, String>("foo").unwrap(), "bar"); assert_eq!(table1.get::<_, String>("baz").unwrap(), "baf"); - lua.load::<()>( + lua.exec::<()>( r#" table1 = {1, 2, 3, 4, 5} table2 = {} @@ -124,7 +124,7 @@ fn test_table() { fn test_function() { let lua = Lua::new(); let globals = lua.globals().unwrap(); - lua.load::<()>( + lua.exec::<()>( r#" function concat(arg1, arg2) return arg1 .. arg2 @@ -144,7 +144,7 @@ fn test_function() { fn test_bind() { let lua = Lua::new(); let globals = lua.globals().unwrap(); - lua.load::<()>( + lua.exec::<()>( r#" function concat(...) local res = "" @@ -171,7 +171,7 @@ fn test_bind() { fn test_rust_function() { let lua = Lua::new(); let globals = lua.globals().unwrap(); - lua.load::<()>( + lua.exec::<()>( r#" function lua_function() return rust_function() @@ -230,7 +230,7 @@ fn test_methods() { let globals = lua.globals().unwrap(); let userdata = lua.create_userdata(UserData(42)).unwrap(); globals.set("userdata", userdata.clone()).unwrap(); - lua.load::<()>( + lua.exec::<()>( r#" function get_it() return userdata:get_value() @@ -293,7 +293,7 @@ fn test_metamethods() { fn test_scope() { let lua = Lua::new(); let globals = lua.globals().unwrap(); - lua.load::<()>( + lua.exec::<()>( r#" touter = { tin = {1, 2, 3} @@ -329,7 +329,7 @@ fn test_scope() { fn test_lua_multi() { let lua = Lua::new(); let globals = lua.globals().unwrap(); - lua.load::<()>( + lua.exec::<()>( r#" function concat(arg1, arg2) return arg1 .. arg2 @@ -360,7 +360,7 @@ fn test_lua_multi() { fn test_coercion() { let lua = Lua::new(); let globals = lua.globals().unwrap(); - lua.load::<()>( + lua.exec::<()>( r#" int = 123 str = "123" @@ -397,7 +397,7 @@ fn test_error() { let lua = Lua::new(); let globals = lua.globals().unwrap(); - lua.load::<()>( + lua.exec::<()>( r#" function no_error() end @@ -472,7 +472,7 @@ fn test_error() { match catch_unwind(|| -> LuaResult<()> { let lua = Lua::new(); - lua.load::<()>( + lua.exec::<()>( r#" function rust_panic() pcall(function () rust_panic_function() end) @@ -496,7 +496,7 @@ fn test_error() { match catch_unwind(|| -> LuaResult<()> { let lua = Lua::new(); - lua.load::<()>( + lua.exec::<()>( r#" function rust_panic() xpcall(function() rust_panic_function() end, function() end) @@ -605,7 +605,7 @@ fn test_thread() { fn test_lightuserdata() { let lua = Lua::new(); let globals = lua.globals().unwrap(); - lua.load::<()>( + lua.exec::<()>( r#" function id(a) return a @@ -625,7 +625,7 @@ fn test_lightuserdata() { fn test_table_error() { let lua = Lua::new(); let globals = lua.globals().unwrap(); - lua.load::<()>( + lua.exec::<()>( r#" table = {} setmetatable(table, { @@ -671,7 +671,7 @@ fn test_result_conversions() { globals.set("err", err).unwrap(); globals.set("ok", ok).unwrap(); - lua.load::<()>( + lua.exec::<()>( r#" local err, msg = err() assert(err == nil) From ed210e88b56fee2fb92ff502fb4d052e8a9ad9d2 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sun, 18 Jun 2017 14:36:30 +0200 Subject: [PATCH 2/4] Rename `LuaTable::(raw_)length` to `(raw_)len` All Rust containers use `len` for this. Even in the Lua API, this is called `lua_len` and `lua_rawlen`. --- examples/examples.rs | 2 +- src/lua.rs | 4 ++-- src/tests.rs | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/examples.rs b/examples/examples.rs index df3abe9..812f74c 100644 --- a/examples/examples.rs +++ b/examples/examples.rs @@ -47,7 +47,7 @@ fn examples() -> LuaResult<()> { array_table.set(1, "one")?; array_table.set(2, "two")?; array_table.set(3, "three")?; - assert_eq!(array_table.length()?, 3); + assert_eq!(array_table.len()?, 3); let map_table = lua.create_empty_table()?; map_table.set("one", 1)?; diff --git a/src/lua.rs b/src/lua.rs index 15442ff..58b9830 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -285,7 +285,7 @@ impl<'lua> LuaTable<'lua> { /// /// This might invoke the `__len` metamethod. Use the `raw_length` method if that is not /// desired. - pub fn length(&self) -> LuaResult { + pub fn len(&self) -> LuaResult { let lua = self.0.lua; unsafe { error_guard(lua.state, 0, 0, |state| { @@ -298,7 +298,7 @@ impl<'lua> LuaTable<'lua> { /// Returns the result of the Lua `#` operator, without invoking the /// `__len` metamethod. - pub fn raw_length(&self) -> LuaResult { + pub fn raw_len(&self) -> LuaResult { let lua = self.0.lua; unsafe { stack_guard(lua.state, 0, || { diff --git a/src/tests.rs b/src/tests.rs index 801db5e..e99519b 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -93,15 +93,15 @@ fn test_table() { let table2 = globals.get::<_, LuaTable>("table2").unwrap(); let table3 = globals.get::<_, LuaTable>("table3").unwrap(); - assert_eq!(table1.length().unwrap(), 5); + assert_eq!(table1.len().unwrap(), 5); assert_eq!( table1.pairs::().unwrap(), vec![(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)] ); - assert_eq!(table2.length().unwrap(), 0); + assert_eq!(table2.len().unwrap(), 0); assert_eq!(table2.pairs::().unwrap(), vec![]); assert_eq!(table2.array_values::().unwrap(), vec![]); - assert_eq!(table3.length().unwrap(), 5); + assert_eq!(table3.len().unwrap(), 5); assert_eq!( table3.array_values::>().unwrap(), vec![Some(1), Some(2), None, Some(4), Some(5)] @@ -646,10 +646,10 @@ fn test_table_error() { let bad_table: LuaTable = globals.get("table").unwrap(); assert!(bad_table.set(1, 1).is_err()); assert!(bad_table.get::<_, i32>(1).is_err()); - assert!(bad_table.length().is_err()); + assert!(bad_table.len().is_err()); assert!(bad_table.raw_set(1, 1).is_ok()); assert!(bad_table.raw_get::<_, i32>(1).is_ok()); - assert_eq!(bad_table.raw_length().unwrap(), 1); + assert_eq!(bad_table.raw_len().unwrap(), 1); assert!(bad_table.pairs::().is_ok()); assert!(bad_table.array_values::().is_ok()); } From a47a6e32efae028c65b1fb17cb4a5f6f2f8cf17b Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sun, 18 Jun 2017 14:39:18 +0200 Subject: [PATCH 3/4] Rename `LuaTable::has` to `contains_key` This is what all Rust maps use --- src/lua.rs | 2 +- src/tests.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lua.rs b/src/lua.rs index 58b9830..56ce092 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -233,7 +233,7 @@ impl<'lua> LuaTable<'lua> { } /// Checks whether the table contains a non-nil value for `key`. - pub fn has>(&self, key: K) -> LuaResult { + pub fn contains_key>(&self, key: K) -> LuaResult { let lua = self.0.lua; let key = key.to_lua(lua)?; unsafe { diff --git a/src/tests.rs b/src/tests.rs index e99519b..8881ce3 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -40,7 +40,7 @@ fn test_load() { "#, None, ).unwrap(); - assert!(module.has("func").unwrap()); + assert!(module.contains_key("func").unwrap()); assert_eq!( module .get::<_, LuaFunction>("func") From 16ae4720d67f214f17f11c05cd8cd0b3228d206c Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sun, 18 Jun 2017 14:48:45 +0200 Subject: [PATCH 4/4] Rename `LuaString::get` to `to_str` This is what similar types in libstd do (`CStr::to_str`, `OsStr::to_str`, `Path::to_str`). --- src/conversion.rs | 2 +- src/lua.rs | 2 +- src/tests.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/conversion.rs b/src/conversion.rs index aa3b1ca..b4a90a5 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -143,7 +143,7 @@ impl<'lua> ToLua<'lua> for String { impl<'lua> FromLua<'lua> for String { fn from_lua(value: LuaValue<'lua>, lua: &'lua Lua) -> LuaResult { - Ok(lua.coerce_string(value)?.get()?.to_owned()) + Ok(lua.coerce_string(value)?.to_str()?.to_owned()) } } diff --git a/src/lua.rs b/src/lua.rs index 56ce092..b2f8e49 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -168,7 +168,7 @@ pub struct LuaString<'lua>(LuaRef<'lua>); impl<'lua> LuaString<'lua> { /// Get a `&str` slice if the Lua string is valid UTF-8. - pub fn get(&self) -> LuaResult<&str> { + pub fn to_str(&self) -> LuaResult<&str> { let lua = self.0.lua; unsafe { stack_guard(lua.state, 0, || { diff --git a/src/tests.rs b/src/tests.rs index 8881ce3..610aec3 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -269,7 +269,7 @@ fn test_metamethods() { }); methods.add_meta_method(LuaMetaMethod::Index, |lua, data, args| { let index = lua.unpack::(args)?; - if index.get()? == "inner" { + if index.to_str()? == "inner" { lua.pack(data.0) } else { Err("no such custom index".into())