This commit is contained in:
Filip Tibell 2023-01-22 16:36:34 -05:00
parent 706368a462
commit 02fd4e8733
No known key found for this signature in database

View file

@ -5,6 +5,8 @@ use tokio::time::{self, Instant};
use crate::utils::table_builder::ReadonlyTableBuilder; use crate::utils::table_builder::ReadonlyTableBuilder;
type Vararg<'lua> = Variadic<Value<'lua>>;
pub async fn create(lua: &Lua) -> Result<()> { pub async fn create(lua: &Lua) -> Result<()> {
lua.globals().raw_set( lua.globals().raw_set(
"task", "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<Thread<'a>> { fn get_or_create_thread_from_arg<'a>(lua: &'a Lua, arg: Value<'a>) -> Result<Thread<'a>> {
Ok(match thread_or_function_arg { Ok(match arg {
Value::Thread(thread) => thread, Value::Thread(thread) => thread,
Value::Function(func) => lua.create_thread(func)?, Value::Function(func) => lua.create_thread(func)?,
val => { val => {
@ -38,28 +40,28 @@ async fn task_cancel(lua: &Lua, thread: Thread<'_>) -> Result<()> {
Ok(()) Ok(())
} }
async fn task_defer<'a>(lua: &Lua, (tof, args): (Value<'a>, Variadic<Value<'a>>)) -> Result<()> { async fn task_defer<'a>(lua: &Lua, (tof, args): (Value<'a>, Vararg<'a>)) -> Result<()> {
task_wait(lua, None).await?; task_wait(lua, None).await?;
get_thread_from_arg(lua, tof)? get_or_create_thread_from_arg(lua, tof)?
.into_async::<_, Variadic<Value<'_>>>(args) .into_async::<_, Vararg<'_>>(args)
.await?; .await?;
Ok(()) Ok(())
} }
async fn task_delay<'a>( async fn task_delay<'a>(
lua: &Lua, lua: &Lua,
(delay, tof, args): (Option<f32>, Value<'a>, Variadic<Value<'a>>), (delay, tof, args): (Option<f32>, Value<'a>, Vararg<'a>),
) -> Result<()> { ) -> Result<()> {
task_wait(lua, delay).await?; task_wait(lua, delay).await?;
get_thread_from_arg(lua, tof)? get_or_create_thread_from_arg(lua, tof)?
.into_async::<_, Variadic<Value<'_>>>(args) .into_async::<_, Vararg<'_>>(args)
.await?; .await?;
Ok(()) Ok(())
} }
async fn task_spawn<'a>(lua: &Lua, (tof, args): (Value<'a>, Variadic<Value<'a>>)) -> Result<()> { async fn task_spawn<'a>(lua: &Lua, (tof, args): (Value<'a>, Vararg<'a>)) -> Result<()> {
get_thread_from_arg(lua, tof)? get_or_create_thread_from_arg(lua, tof)?
.into_async::<_, Variadic<Value<'_>>>(args) .into_async::<_, Vararg<'_>>(args)
.await?; .await?;
Ok(()) Ok(())
} }