Methods take owned threads

This commit is contained in:
Filip Tibell 2023-08-17 21:37:09 -05:00
parent ab386e000d
commit 1b7287a742
4 changed files with 13 additions and 34 deletions

View file

@ -44,11 +44,15 @@ impl Lune {
let lua = Arc::new(Lua::new());
let sched = Scheduler::new(Arc::clone(&lua));
let main = lua
let main_fn = lua
.load(script_contents.as_ref())
.set_name(script_name.as_ref());
sched.push_front(main, ())?;
.set_name(script_name.as_ref())
.into_function()?;
let main_thread = lua.create_thread(main_fn)?.into_owned();
sched
.push_back(main_thread, ())
.expect("Failed to enqueue thread for main");
Ok(sched.run_to_completion().await)
}
}

View file

@ -1,7 +1,7 @@
use futures_util::Future;
use mlua::prelude::*;
use super::{traits::IntoLuaOwnedThread, SchedulerImpl};
use super::SchedulerImpl;
impl<'lua, 'fut> SchedulerImpl<'fut>
where
@ -10,7 +10,7 @@ where
/**
Schedules a plain future to run whenever the scheduler is available.
*/
pub fn schedule_future<F>(&self, fut: F)
pub fn schedule_future<F>(&'lua self, fut: F)
where
F: 'fut + Future<Output = ()>,
{
@ -24,15 +24,11 @@ where
/**
Schedules the given `thread` to run when the given `fut` completes.
*/
pub fn schedule_future_thread<T, R, F>(&'fut self, thread: T, fut: F) -> LuaResult<()>
pub fn schedule_future_thread<R, F>(&'lua self, thread: LuaOwnedThread, fut: F) -> LuaResult<()>
where
T: IntoLuaOwnedThread,
R: IntoLuaMulti<'fut>,
F: 'fut + Future<Output = LuaResult<R>>,
{
let thread = thread.into_owned_lua_thread(&self.lua)?;
// FIXME: We use self in the future below, so this doesn't compile... how to fix?
self.schedule_future(async move {
let rets = fut.await.expect("Failed to receive result");
self.push_back(thread, rets)

View file

@ -4,7 +4,7 @@ use futures_util::StreamExt;
use mlua::prelude::*;
use tokio::task::LocalSet;
use super::{traits::IntoLuaOwnedThread, SchedulerImpl};
use super::SchedulerImpl;
impl<'lua, 'fut> SchedulerImpl<'fut>
where
@ -119,22 +119,4 @@ where
ExitCode::SUCCESS
}
}
/**
Schedules a new main thread and runs the scheduler until completion.
See [`Self::run_to_completion`] for more info.
*/
pub async fn run_main(
&'lua self,
main: impl IntoLuaOwnedThread,
args: impl IntoLuaMulti<'lua>,
) -> ExitCode {
let thread = main
.into_owned_lua_thread(&self.lua)
.expect("Failed to create thread for main");
self.push_back(thread, args)
.expect("Failed to queue thread for main");
self.run_to_completion().await
}
}

View file

@ -4,7 +4,6 @@ use mlua::prelude::*;
use super::{
thread::{SchedulerThread, SchedulerThreadId, SchedulerThreadSender},
traits::IntoLuaOwnedThread,
SchedulerImpl,
};
@ -58,10 +57,9 @@ where
*/
pub fn push_front(
&'lua self,
thread: impl IntoLuaOwnedThread,
thread: LuaOwnedThread,
args: impl IntoLuaMulti<'lua>,
) -> LuaResult<SchedulerThreadId> {
let thread = thread.into_owned_lua_thread(&self.lua)?;
let args = args.into_lua_multi(&self.lua)?;
let thread = SchedulerThread::new(&self.lua, thread, args)?;
@ -85,10 +83,9 @@ where
*/
pub fn push_back(
&'lua self,
thread: impl IntoLuaOwnedThread,
thread: LuaOwnedThread,
args: impl IntoLuaMulti<'lua>,
) -> LuaResult<SchedulerThreadId> {
let thread = thread.into_owned_lua_thread(&self.lua)?;
let args = args.into_lua_multi(&self.lua)?;
let thread = SchedulerThread::new(&self.lua, thread, args)?;