Merge pull request #10 from jonas-schievink/api-revamp
Rename a few methods to better match their intention or conventions
This commit is contained in:
commit
5b148536d6
|
@ -29,7 +29,7 @@ fn examples() -> LuaResult<()> {
|
||||||
// You can load and evaluate lua code. The second parameter here gives the
|
// You can load and evaluate lua code. The second parameter here gives the
|
||||||
// chunk a better name when lua error messages are printed.
|
// chunk a better name when lua error messages are printed.
|
||||||
|
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
global = 'foo'..'bar'
|
global = 'foo'..'bar'
|
||||||
"#,
|
"#,
|
||||||
|
@ -47,7 +47,7 @@ fn examples() -> LuaResult<()> {
|
||||||
array_table.set(1, "one")?;
|
array_table.set(1, "one")?;
|
||||||
array_table.set(2, "two")?;
|
array_table.set(2, "two")?;
|
||||||
array_table.set(3, "three")?;
|
array_table.set(3, "three")?;
|
||||||
assert_eq!(array_table.length()?, 3);
|
assert_eq!(array_table.len()?, 3);
|
||||||
|
|
||||||
let map_table = lua.create_empty_table()?;
|
let map_table = lua.create_empty_table()?;
|
||||||
map_table.set("one", 1)?;
|
map_table.set("one", 1)?;
|
||||||
|
|
|
@ -143,7 +143,7 @@ impl<'lua> ToLua<'lua> for String {
|
||||||
|
|
||||||
impl<'lua> FromLua<'lua> for String {
|
impl<'lua> FromLua<'lua> for String {
|
||||||
fn from_lua(value: LuaValue<'lua>, lua: &'lua Lua) -> LuaResult<Self> {
|
fn from_lua(value: LuaValue<'lua>, lua: &'lua Lua) -> LuaResult<Self> {
|
||||||
Ok(lua.coerce_string(value)?.get()?.to_owned())
|
Ok(lua.coerce_string(value)?.to_str()?.to_owned())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
src/lua.rs
10
src/lua.rs
|
@ -168,7 +168,7 @@ pub struct LuaString<'lua>(LuaRef<'lua>);
|
||||||
|
|
||||||
impl<'lua> LuaString<'lua> {
|
impl<'lua> LuaString<'lua> {
|
||||||
/// Get a `&str` slice if the Lua string is valid UTF-8.
|
/// 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;
|
let lua = self.0.lua;
|
||||||
unsafe {
|
unsafe {
|
||||||
stack_guard(lua.state, 0, || {
|
stack_guard(lua.state, 0, || {
|
||||||
|
@ -233,7 +233,7 @@ impl<'lua> LuaTable<'lua> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks whether the table contains a non-nil value for `key`.
|
/// Checks whether the table contains a non-nil value for `key`.
|
||||||
pub fn has<K: ToLua<'lua>>(&self, key: K) -> LuaResult<bool> {
|
pub fn contains_key<K: ToLua<'lua>>(&self, key: K) -> LuaResult<bool> {
|
||||||
let lua = self.0.lua;
|
let lua = self.0.lua;
|
||||||
let key = key.to_lua(lua)?;
|
let key = key.to_lua(lua)?;
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -285,7 +285,7 @@ impl<'lua> LuaTable<'lua> {
|
||||||
///
|
///
|
||||||
/// This might invoke the `__len` metamethod. Use the `raw_length` method if that is not
|
/// This might invoke the `__len` metamethod. Use the `raw_length` method if that is not
|
||||||
/// desired.
|
/// desired.
|
||||||
pub fn length(&self) -> LuaResult<LuaInteger> {
|
pub fn len(&self) -> LuaResult<LuaInteger> {
|
||||||
let lua = self.0.lua;
|
let lua = self.0.lua;
|
||||||
unsafe {
|
unsafe {
|
||||||
error_guard(lua.state, 0, 0, |state| {
|
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
|
/// Returns the result of the Lua `#` operator, without invoking the
|
||||||
/// `__len` metamethod.
|
/// `__len` metamethod.
|
||||||
pub fn raw_length(&self) -> LuaResult<LuaInteger> {
|
pub fn raw_len(&self) -> LuaResult<LuaInteger> {
|
||||||
let lua = self.0.lua;
|
let lua = self.0.lua;
|
||||||
unsafe {
|
unsafe {
|
||||||
stack_guard(lua.state, 0, || {
|
stack_guard(lua.state, 0, || {
|
||||||
|
@ -905,7 +905,7 @@ impl Lua {
|
||||||
/// results in better error traces.
|
/// results in better error traces.
|
||||||
///
|
///
|
||||||
/// Returns the values returned by the chunk.
|
/// Returns the values returned by the chunk.
|
||||||
pub fn load<'lua, R: FromLuaMulti<'lua>>(
|
pub fn exec<'lua, R: FromLuaMulti<'lua>>(
|
||||||
&'lua self,
|
&'lua self,
|
||||||
source: &str,
|
source: &str,
|
||||||
name: Option<&str>,
|
name: Option<&str>,
|
||||||
|
|
46
src/tests.rs
46
src/tests.rs
|
@ -20,7 +20,7 @@ fn test_set_get() {
|
||||||
fn test_load() {
|
fn test_load() {
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
let globals = lua.globals().unwrap();
|
let globals = lua.globals().unwrap();
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
res = 'foo'..'bar'
|
res = 'foo'..'bar'
|
||||||
"#,
|
"#,
|
||||||
|
@ -28,7 +28,7 @@ fn test_load() {
|
||||||
).unwrap();
|
).unwrap();
|
||||||
assert_eq!(globals.get::<_, String>("res").unwrap(), "foobar");
|
assert_eq!(globals.get::<_, String>("res").unwrap(), "foobar");
|
||||||
|
|
||||||
let module: LuaTable = lua.load(
|
let module: LuaTable = lua.exec(
|
||||||
r#"
|
r#"
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ fn test_load() {
|
||||||
"#,
|
"#,
|
||||||
None,
|
None,
|
||||||
).unwrap();
|
).unwrap();
|
||||||
assert!(module.has("func").unwrap());
|
assert!(module.contains_key("func").unwrap());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
module
|
module
|
||||||
.get::<_, LuaFunction>("func")
|
.get::<_, LuaFunction>("func")
|
||||||
|
@ -80,7 +80,7 @@ fn test_table() {
|
||||||
assert_eq!(table2.get::<_, String>("foo").unwrap(), "bar");
|
assert_eq!(table2.get::<_, String>("foo").unwrap(), "bar");
|
||||||
assert_eq!(table1.get::<_, String>("baz").unwrap(), "baf");
|
assert_eq!(table1.get::<_, String>("baz").unwrap(), "baf");
|
||||||
|
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
table1 = {1, 2, 3, 4, 5}
|
table1 = {1, 2, 3, 4, 5}
|
||||||
table2 = {}
|
table2 = {}
|
||||||
|
@ -93,15 +93,15 @@ fn test_table() {
|
||||||
let table2 = globals.get::<_, LuaTable>("table2").unwrap();
|
let table2 = globals.get::<_, LuaTable>("table2").unwrap();
|
||||||
let table3 = globals.get::<_, LuaTable>("table3").unwrap();
|
let table3 = globals.get::<_, LuaTable>("table3").unwrap();
|
||||||
|
|
||||||
assert_eq!(table1.length().unwrap(), 5);
|
assert_eq!(table1.len().unwrap(), 5);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
table1.pairs::<i64, i64>().unwrap(),
|
table1.pairs::<i64, i64>().unwrap(),
|
||||||
vec![(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
|
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::<i64, i64>().unwrap(), vec![]);
|
assert_eq!(table2.pairs::<i64, i64>().unwrap(), vec![]);
|
||||||
assert_eq!(table2.array_values::<i64>().unwrap(), vec![]);
|
assert_eq!(table2.array_values::<i64>().unwrap(), vec![]);
|
||||||
assert_eq!(table3.length().unwrap(), 5);
|
assert_eq!(table3.len().unwrap(), 5);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
table3.array_values::<Option<i64>>().unwrap(),
|
table3.array_values::<Option<i64>>().unwrap(),
|
||||||
vec![Some(1), Some(2), None, Some(4), Some(5)]
|
vec![Some(1), Some(2), None, Some(4), Some(5)]
|
||||||
|
@ -124,7 +124,7 @@ fn test_table() {
|
||||||
fn test_function() {
|
fn test_function() {
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
let globals = lua.globals().unwrap();
|
let globals = lua.globals().unwrap();
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
function concat(arg1, arg2)
|
function concat(arg1, arg2)
|
||||||
return arg1 .. arg2
|
return arg1 .. arg2
|
||||||
|
@ -144,7 +144,7 @@ fn test_function() {
|
||||||
fn test_bind() {
|
fn test_bind() {
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
let globals = lua.globals().unwrap();
|
let globals = lua.globals().unwrap();
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
function concat(...)
|
function concat(...)
|
||||||
local res = ""
|
local res = ""
|
||||||
|
@ -171,7 +171,7 @@ fn test_bind() {
|
||||||
fn test_rust_function() {
|
fn test_rust_function() {
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
let globals = lua.globals().unwrap();
|
let globals = lua.globals().unwrap();
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
function lua_function()
|
function lua_function()
|
||||||
return rust_function()
|
return rust_function()
|
||||||
|
@ -230,7 +230,7 @@ fn test_methods() {
|
||||||
let globals = lua.globals().unwrap();
|
let globals = lua.globals().unwrap();
|
||||||
let userdata = lua.create_userdata(UserData(42)).unwrap();
|
let userdata = lua.create_userdata(UserData(42)).unwrap();
|
||||||
globals.set("userdata", userdata.clone()).unwrap();
|
globals.set("userdata", userdata.clone()).unwrap();
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
function get_it()
|
function get_it()
|
||||||
return userdata:get_value()
|
return userdata:get_value()
|
||||||
|
@ -269,7 +269,7 @@ fn test_metamethods() {
|
||||||
});
|
});
|
||||||
methods.add_meta_method(LuaMetaMethod::Index, |lua, data, args| {
|
methods.add_meta_method(LuaMetaMethod::Index, |lua, data, args| {
|
||||||
let index = lua.unpack::<LuaString>(args)?;
|
let index = lua.unpack::<LuaString>(args)?;
|
||||||
if index.get()? == "inner" {
|
if index.to_str()? == "inner" {
|
||||||
lua.pack(data.0)
|
lua.pack(data.0)
|
||||||
} else {
|
} else {
|
||||||
Err("no such custom index".into())
|
Err("no such custom index".into())
|
||||||
|
@ -293,7 +293,7 @@ fn test_metamethods() {
|
||||||
fn test_scope() {
|
fn test_scope() {
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
let globals = lua.globals().unwrap();
|
let globals = lua.globals().unwrap();
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
touter = {
|
touter = {
|
||||||
tin = {1, 2, 3}
|
tin = {1, 2, 3}
|
||||||
|
@ -329,7 +329,7 @@ fn test_scope() {
|
||||||
fn test_lua_multi() {
|
fn test_lua_multi() {
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
let globals = lua.globals().unwrap();
|
let globals = lua.globals().unwrap();
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
function concat(arg1, arg2)
|
function concat(arg1, arg2)
|
||||||
return arg1 .. arg2
|
return arg1 .. arg2
|
||||||
|
@ -360,7 +360,7 @@ fn test_lua_multi() {
|
||||||
fn test_coercion() {
|
fn test_coercion() {
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
let globals = lua.globals().unwrap();
|
let globals = lua.globals().unwrap();
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
int = 123
|
int = 123
|
||||||
str = "123"
|
str = "123"
|
||||||
|
@ -397,7 +397,7 @@ fn test_error() {
|
||||||
|
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
let globals = lua.globals().unwrap();
|
let globals = lua.globals().unwrap();
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
function no_error()
|
function no_error()
|
||||||
end
|
end
|
||||||
|
@ -472,7 +472,7 @@ fn test_error() {
|
||||||
|
|
||||||
match catch_unwind(|| -> LuaResult<()> {
|
match catch_unwind(|| -> LuaResult<()> {
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
function rust_panic()
|
function rust_panic()
|
||||||
pcall(function () rust_panic_function() end)
|
pcall(function () rust_panic_function() end)
|
||||||
|
@ -496,7 +496,7 @@ fn test_error() {
|
||||||
|
|
||||||
match catch_unwind(|| -> LuaResult<()> {
|
match catch_unwind(|| -> LuaResult<()> {
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
function rust_panic()
|
function rust_panic()
|
||||||
xpcall(function() rust_panic_function() end, function() end)
|
xpcall(function() rust_panic_function() end, function() end)
|
||||||
|
@ -605,7 +605,7 @@ fn test_thread() {
|
||||||
fn test_lightuserdata() {
|
fn test_lightuserdata() {
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
let globals = lua.globals().unwrap();
|
let globals = lua.globals().unwrap();
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
function id(a)
|
function id(a)
|
||||||
return a
|
return a
|
||||||
|
@ -625,7 +625,7 @@ fn test_lightuserdata() {
|
||||||
fn test_table_error() {
|
fn test_table_error() {
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
let globals = lua.globals().unwrap();
|
let globals = lua.globals().unwrap();
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
table = {}
|
table = {}
|
||||||
setmetatable(table, {
|
setmetatable(table, {
|
||||||
|
@ -646,10 +646,10 @@ fn test_table_error() {
|
||||||
let bad_table: LuaTable = globals.get("table").unwrap();
|
let bad_table: LuaTable = globals.get("table").unwrap();
|
||||||
assert!(bad_table.set(1, 1).is_err());
|
assert!(bad_table.set(1, 1).is_err());
|
||||||
assert!(bad_table.get::<_, i32>(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_set(1, 1).is_ok());
|
||||||
assert!(bad_table.raw_get::<_, i32>(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::<i64, i64>().is_ok());
|
assert!(bad_table.pairs::<i64, i64>().is_ok());
|
||||||
assert!(bad_table.array_values::<i64>().is_ok());
|
assert!(bad_table.array_values::<i64>().is_ok());
|
||||||
}
|
}
|
||||||
|
@ -671,7 +671,7 @@ fn test_result_conversions() {
|
||||||
globals.set("err", err).unwrap();
|
globals.set("err", err).unwrap();
|
||||||
globals.set("ok", ok).unwrap();
|
globals.set("ok", ok).unwrap();
|
||||||
|
|
||||||
lua.load::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
local err, msg = err()
|
local err, msg = err()
|
||||||
assert(err == nil)
|
assert(err == nil)
|
||||||
|
|
Loading…
Reference in New Issue