Skip to content

Commit

Permalink
Impl push_into_stack for StdResult
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Feb 1, 2024
1 parent f5982bc commit f4d783c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 38 deletions.
28 changes: 28 additions & 0 deletions src/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ impl<'lua, T: IntoLua<'lua>, E: IntoLua<'lua>> IntoLuaMulti<'lua> for StdResult<
}
Ok(result)
}

#[inline]
unsafe fn push_into_stack_multi(self, lua: &'lua Lua) -> Result<c_int> {
match self {
Ok(v) => v.push_into_stack(lua).map(|_| 1),
Err(e) => {
let state = lua.state();
check_stack(state, 3)?;
ffi::lua_pushnil(state);
e.push_into_stack(lua)?;
Ok(2)
}
}
}
}

impl<'lua, E: IntoLua<'lua>> IntoLuaMulti<'lua> for StdResult<(), E> {
Expand All @@ -38,6 +52,20 @@ impl<'lua, E: IntoLua<'lua>> IntoLuaMulti<'lua> for StdResult<(), E> {
}
}
}

#[inline]
unsafe fn push_into_stack_multi(self, lua: &'lua Lua) -> Result<c_int> {
match self {
Ok(_) => Ok(0),
Err(e) => {
let state = lua.state();
check_stack(state, 3)?;
ffi::lua_pushnil(state);
e.push_into_stack(lua)?;
Ok(2)
}
}
}
}

impl<'lua, T: IntoLua<'lua>> IntoLuaMulti<'lua> for T {
Expand Down
60 changes: 60 additions & 0 deletions tests/multi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use mlua::{Error, ExternalError, IntoLuaMulti, Lua, Result, String, Value};

#[test]
fn test_result_conversions() -> Result<()> {
let lua = Lua::new();
let globals = lua.globals();

let ok = lua.create_function(|_, ()| Ok(Ok::<(), Error>(())))?;
let err = lua.create_function(|_, ()| Ok(Err::<(), _>("failure1".into_lua_err())))?;
let ok2 = lua.create_function(|_, ()| Ok(Ok::<_, Error>("!".to_owned())))?;
let err2 = lua.create_function(|_, ()| Ok(Err::<String, _>("failure2".into_lua_err())))?;

globals.set("ok", ok)?;
globals.set("ok2", ok2)?;
globals.set("err", err)?;
globals.set("err2", err2)?;

lua.load(
r#"
local r, e = ok()
assert(r == nil and e == nil)
local r, e = err()
assert(r == nil)
assert(tostring(e):find("failure1") ~= nil)
local r, e = ok2()
assert(r == "!")
assert(e == nil)
local r, e = err2()
assert(r == nil)
assert(tostring(e):find("failure2") ~= nil)
"#,
)
.exec()?;

// Try to convert Result into MultiValue
let ok1 = Ok::<(), Error>(());
let multi_ok1 = ok1.into_lua_multi(&lua)?;
assert_eq!(multi_ok1.len(), 0);
let err1 = Err::<(), _>("failure1");
let multi_err1 = err1.into_lua_multi(&lua)?;
assert_eq!(multi_err1.len(), 2);
assert_eq!(multi_err1[0], Value::Nil);
assert_eq!(multi_err1[1].as_str().unwrap(), "failure1");

let ok2 = Ok::<_, Error>("!");
let multi_ok2 = ok2.into_lua_multi(&lua)?;
assert_eq!(multi_ok2.len(), 1);
assert_eq!(multi_ok2[0].as_str().unwrap(), "!");
let err2 = Err::<String, _>("failure2".into_lua_err());
let multi_err2 = err2.into_lua_multi(&lua)?;
assert_eq!(multi_err2.len(), 2);
assert_eq!(multi_err2[0], Value::Nil);
assert!(matches!(multi_err2[1], Value::Error(_)));
assert_eq!(multi_err2[1].to_string()?, "failure2");

Ok(())
}
38 changes: 0 additions & 38 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,44 +504,6 @@ fn test_panic() -> Result<()> {
Ok(())
}

#[test]
fn test_result_conversions() -> Result<()> {
let lua = Lua::new();
let globals = lua.globals();

let ok = lua.create_function(|_, ()| Ok(Ok::<(), Error>(())))?;
let err = lua.create_function(|_, ()| Ok(Err::<(), _>("failure1".into_lua_err())))?;
let ok2 = lua.create_function(|_, ()| Ok(Ok::<_, Error>("!".to_owned())))?;
let err2 = lua.create_function(|_, ()| Ok(Err::<String, _>("failure2".into_lua_err())))?;

globals.set("ok", ok)?;
globals.set("ok2", ok2)?;
globals.set("err", err)?;
globals.set("err2", err2)?;

lua.load(
r#"
local r, e = ok()
assert(r == nil and e == nil)
local r, e = err()
assert(r == nil)
assert(tostring(e):find("failure1") ~= nil)
local r, e = ok2()
assert(r == "!")
assert(e == nil)
local r, e = err2()
assert(r == nil)
assert(tostring(e):find("failure2") ~= nil)
"#,
)
.exec()?;

Ok(())
}

#[test]
fn test_num_conversion() -> Result<()> {
let lua = Lua::new();
Expand Down

0 comments on commit f4d783c

Please sign in to comment.