Struct mlua_luau_scheduler::Scheduler
source · pub struct Scheduler<'lua> { /* private fields */ }
Expand description
A scheduler for running Lua threads and async tasks.
Implementations§
source§impl<'lua> Scheduler<'lua>
impl<'lua> Scheduler<'lua>
sourcepub fn new(lua: &'lua Lua) -> Scheduler<'lua>
pub fn new(lua: &'lua Lua) -> Scheduler<'lua>
Creates a new scheduler for the given Lua state.
This scheduler will have a default error callback that prints errors to stderr.
§Panics
Panics if the given Lua state already has a scheduler attached to it.
sourcepub fn set_error_callback(&self, callback: impl Fn(LuaError) + Send + 'static)
pub fn set_error_callback(&self, callback: impl Fn(LuaError) + Send + 'static)
Sets the error callback for this scheduler.
This callback will be called whenever a Lua thread errors.
Overwrites any previous error callback.
§Panics
Panics if the scheduler is currently running.
sourcepub fn remove_error_callback(&self)
pub fn remove_error_callback(&self)
Clears the error callback for this scheduler.
This will remove any current error callback, including default(s).
§Panics
Panics if the scheduler is currently running.
sourcepub fn get_exit_code(&self) -> Option<ExitCode>
pub fn get_exit_code(&self) -> Option<ExitCode>
Gets the exit code for this scheduler, if one has been set.
sourcepub fn set_exit_code(&self, code: ExitCode)
pub fn set_exit_code(&self, code: ExitCode)
Sets the exit code for this scheduler.
This will cause Scheduler::run
to exit immediately.
sourcepub fn push_thread_front(
&self,
thread: impl IntoLuaThread<'lua>,
args: impl IntoLuaMulti<'lua>
) -> LuaResult<ThreadId>
pub fn push_thread_front( &self, thread: impl IntoLuaThread<'lua>, args: impl IntoLuaMulti<'lua> ) -> LuaResult<ThreadId>
Spawns a chunk / function / thread onto the scheduler queue.
Threads are guaranteed to be resumed in the order that they were pushed to the queue.
§Returns
Returns a ThreadId
that can be used to retrieve the result of the thread.
Note that the result may not be available until Scheduler::run
completes.
§Errors
Errors when out of memory.
sourcepub fn push_thread_back(
&self,
thread: impl IntoLuaThread<'lua>,
args: impl IntoLuaMulti<'lua>
) -> LuaResult<ThreadId>
pub fn push_thread_back( &self, thread: impl IntoLuaThread<'lua>, args: impl IntoLuaMulti<'lua> ) -> LuaResult<ThreadId>
Defers a chunk / function / thread onto the scheduler queue.
Deferred threads are guaranteed to run after all spawned threads either yield or complete.
Threads are guaranteed to be resumed in the order that they were pushed to the queue.
§Returns
Returns a ThreadId
that can be used to retrieve the result of the thread.
Note that the result may not be available until Scheduler::run
completes.
§Errors
Errors when out of memory.
sourcepub fn get_thread_result(
&self,
id: ThreadId
) -> Option<LuaResult<LuaMultiValue<'lua>>>
pub fn get_thread_result( &self, id: ThreadId ) -> Option<LuaResult<LuaMultiValue<'lua>>>
Gets the tracked result for the LuaThread
with the given ThreadId
.
Depending on the current Scheduler::status
, this method will return:
Status::NotStarted
: returnsNone
.Status::Running
: may returnSome(Ok(v))
orSome(Err(e))
, but it is not guaranteed.Status::Completed
: returnsSome(Ok(v))
orSome(Err(e))
.
Note that this method also takes the value out of the scheduler and stops tracking the given thread, so it may only be called once.
Any subsequent calls after this method returns Some
will return None
.