From f4d783cb414c9af1afbde9439b3d92c27fe952a6 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Thu, 1 Feb 2024 09:30:42 +0000 Subject: [PATCH] Impl push_into_stack for StdResult --- src/multi.rs | 28 +++++++++++++++++++++++ tests/multi.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/tests.rs | 38 -------------------------------- 3 files changed, 88 insertions(+), 38 deletions(-) create mode 100644 tests/multi.rs diff --git a/src/multi.rs b/src/multi.rs index 3e8b4bc9..eaa1107a 100644 --- a/src/multi.rs +++ b/src/multi.rs @@ -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 { + 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> { @@ -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 { + 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 { diff --git a/tests/multi.rs b/tests/multi.rs new file mode 100644 index 00000000..3ee7bdb2 --- /dev/null +++ b/tests/multi.rs @@ -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::("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::("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(()) +} diff --git a/tests/tests.rs b/tests/tests.rs index 0310a824..4d072bb2 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -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::("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();