mirror of
https://github.com/lune-org/mlua-luau-scheduler.git
synced 2025-04-10 21:40:55 +01:00
Expand async ext for creating non-send functions
This commit is contained in:
parent
c344fb927a
commit
c920d31126
1 changed files with 47 additions and 9 deletions
|
@ -1,23 +1,36 @@
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
|
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
use tokio::spawn;
|
use tokio::{spawn, task::spawn_local};
|
||||||
|
|
||||||
use crate::{AsyncValues, Message, MessageSender, ThreadId};
|
use crate::{AsyncValues, Message, MessageSender, ThreadId};
|
||||||
|
|
||||||
pub trait LuaSchedulerExt<'lua> {
|
pub trait LuaAsyncExt<'lua> {
|
||||||
fn create_async_function<A, R, F, FR>(&'lua self, func: F) -> LuaResult<LuaFunction<'lua>>
|
fn current_thread_id(&'lua self) -> ThreadId;
|
||||||
|
|
||||||
|
fn create_async_function<A, R, F, FR>(&'lua self, f: F) -> LuaResult<LuaFunction<'lua>>
|
||||||
where
|
where
|
||||||
A: FromLuaMulti<'lua> + 'static,
|
A: FromLuaMulti<'lua>,
|
||||||
R: Into<AsyncValues> + Send + 'static,
|
R: Into<AsyncValues> + Send + 'static,
|
||||||
F: Fn(&'lua Lua, A) -> FR + 'static,
|
F: Fn(&'lua Lua, A) -> FR + 'static,
|
||||||
FR: Future<Output = LuaResult<R>> + Send + 'static;
|
FR: Future<Output = LuaResult<R>> + Send + 'static;
|
||||||
|
|
||||||
|
fn create_local_async_function<A, R, F, FR>(&'lua self, f: F) -> LuaResult<LuaFunction<'lua>>
|
||||||
|
where
|
||||||
|
A: FromLuaMulti<'lua>,
|
||||||
|
R: Into<AsyncValues> + 'static,
|
||||||
|
F: Fn(&'lua Lua, A) -> FR + 'static,
|
||||||
|
FR: Future<Output = LuaResult<R>> + 'static;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'lua> LuaSchedulerExt<'lua> for Lua {
|
impl<'lua> LuaAsyncExt<'lua> for Lua {
|
||||||
fn create_async_function<A, R, F, FR>(&'lua self, func: F) -> LuaResult<LuaFunction<'lua>>
|
fn current_thread_id(&'lua self) -> ThreadId {
|
||||||
|
ThreadId::from(self.current_thread())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_async_function<A, R, F, FR>(&'lua self, f: F) -> LuaResult<LuaFunction<'lua>>
|
||||||
where
|
where
|
||||||
A: FromLuaMulti<'lua> + 'static,
|
A: FromLuaMulti<'lua>,
|
||||||
R: Into<AsyncValues> + Send + 'static,
|
R: Into<AsyncValues> + Send + 'static,
|
||||||
F: Fn(&'lua Lua, A) -> FR + 'static,
|
F: Fn(&'lua Lua, A) -> FR + 'static,
|
||||||
FR: Future<Output = LuaResult<R>> + Send + 'static,
|
FR: Future<Output = LuaResult<R>> + Send + 'static,
|
||||||
|
@ -25,8 +38,8 @@ impl<'lua> LuaSchedulerExt<'lua> for Lua {
|
||||||
let tx = self.app_data_ref::<MessageSender>().unwrap().clone();
|
let tx = self.app_data_ref::<MessageSender>().unwrap().clone();
|
||||||
|
|
||||||
self.create_function(move |lua, args: A| {
|
self.create_function(move |lua, args: A| {
|
||||||
let thread_id = ThreadId::from(lua.current_thread());
|
let thread_id = lua.current_thread_id();
|
||||||
let fut = func(lua, args);
|
let fut = f(lua, args);
|
||||||
let tx = tx.clone();
|
let tx = tx.clone();
|
||||||
|
|
||||||
spawn(async move {
|
spawn(async move {
|
||||||
|
@ -39,4 +52,29 @@ impl<'lua> LuaSchedulerExt<'lua> for Lua {
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_local_async_function<A, R, F, FR>(&'lua self, f: F) -> LuaResult<LuaFunction<'lua>>
|
||||||
|
where
|
||||||
|
A: FromLuaMulti<'lua>,
|
||||||
|
R: Into<AsyncValues> + 'static,
|
||||||
|
F: Fn(&'lua Lua, A) -> FR + 'static,
|
||||||
|
FR: Future<Output = LuaResult<R>> + 'static,
|
||||||
|
{
|
||||||
|
let tx = self.app_data_ref::<MessageSender>().unwrap().clone();
|
||||||
|
|
||||||
|
self.create_function(move |lua, args: A| {
|
||||||
|
let thread_id = lua.current_thread_id();
|
||||||
|
let fut = f(lua, args);
|
||||||
|
let tx = tx.clone();
|
||||||
|
|
||||||
|
spawn_local(async move {
|
||||||
|
tx.send(match fut.await {
|
||||||
|
Ok(args) => Message::Resume(thread_id, Ok(args.into())),
|
||||||
|
Err(e) => Message::Resume(thread_id, Err(e)),
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue