use mlua::prelude::*; /** Wrapper struct to accept either a Lua thread or a Lua function as function argument. [`LuaThreadOrFunction::into_thread`] may be used to convert the value into a Lua thread. */ #[derive(Clone)] pub(crate) enum LuaThreadOrFunction<'lua> { Thread(LuaThread<'lua>), Function(LuaFunction<'lua>), } impl<'lua> LuaThreadOrFunction<'lua> { pub(super) fn into_thread(self, lua: &'lua Lua) -> LuaResult> { match self { Self::Thread(t) => Ok(t), Self::Function(f) => lua.create_thread(f), } } } impl<'lua> FromLua<'lua> for LuaThreadOrFunction<'lua> { fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult { match value { LuaValue::Thread(t) => Ok(Self::Thread(t)), LuaValue::Function(f) => Ok(Self::Function(f)), value => Err(LuaError::FromLuaConversionError { from: value.type_name(), to: "LuaThreadOrFunction", message: Some("Expected thread or function".to_string()), }), } } }