From 02fd4e87336d29724b148fba6a772b54de950607 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sun, 22 Jan 2023 16:36:34 -0500 Subject: [PATCH] Cleanup --- src/lib/globals/task.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/lib/globals/task.rs b/src/lib/globals/task.rs index f58251b..6941437 100644 --- a/src/lib/globals/task.rs +++ b/src/lib/globals/task.rs @@ -5,6 +5,8 @@ use tokio::time::{self, Instant}; use crate::utils::table_builder::ReadonlyTableBuilder; +type Vararg<'lua> = Variadic>; + pub async fn create(lua: &Lua) -> Result<()> { lua.globals().raw_set( "task", @@ -18,8 +20,8 @@ pub async fn create(lua: &Lua) -> Result<()> { ) } -fn get_thread_from_arg<'a>(lua: &'a Lua, thread_or_function_arg: Value<'a>) -> Result> { - Ok(match thread_or_function_arg { +fn get_or_create_thread_from_arg<'a>(lua: &'a Lua, arg: Value<'a>) -> Result> { + Ok(match arg { Value::Thread(thread) => thread, Value::Function(func) => lua.create_thread(func)?, val => { @@ -38,28 +40,28 @@ async fn task_cancel(lua: &Lua, thread: Thread<'_>) -> Result<()> { Ok(()) } -async fn task_defer<'a>(lua: &Lua, (tof, args): (Value<'a>, Variadic>)) -> Result<()> { +async fn task_defer<'a>(lua: &Lua, (tof, args): (Value<'a>, Vararg<'a>)) -> Result<()> { task_wait(lua, None).await?; - get_thread_from_arg(lua, tof)? - .into_async::<_, Variadic>>(args) + get_or_create_thread_from_arg(lua, tof)? + .into_async::<_, Vararg<'_>>(args) .await?; Ok(()) } async fn task_delay<'a>( lua: &Lua, - (delay, tof, args): (Option, Value<'a>, Variadic>), + (delay, tof, args): (Option, Value<'a>, Vararg<'a>), ) -> Result<()> { task_wait(lua, delay).await?; - get_thread_from_arg(lua, tof)? - .into_async::<_, Variadic>>(args) + get_or_create_thread_from_arg(lua, tof)? + .into_async::<_, Vararg<'_>>(args) .await?; Ok(()) } -async fn task_spawn<'a>(lua: &Lua, (tof, args): (Value<'a>, Variadic>)) -> Result<()> { - get_thread_from_arg(lua, tof)? - .into_async::<_, Variadic>>(args) +async fn task_spawn<'a>(lua: &Lua, (tof, args): (Value<'a>, Vararg<'a>)) -> Result<()> { + get_or_create_thread_from_arg(lua, tof)? + .into_async::<_, Vararg<'_>>(args) .await?; Ok(()) }