pub struct Scheduler { /* private fields */ }Expand description
A scheduler for running Lua threads and async tasks.
Implementations§
Source§impl Scheduler
impl Scheduler
Sourcepub fn new(lua: Lua) -> Scheduler
pub fn new(lua: Lua) -> Scheduler
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<u8>
pub fn get_exit_code(&self) -> Option<u8>
Gets the exit code for this scheduler, if one has been set.
Sourcepub fn set_exit_code(&self, code: u8)
pub fn set_exit_code(&self, code: u8)
Sets the exit code for this scheduler.
This will cause Scheduler::run to exit immediately.
Sourcepub fn push_thread_front(
&self,
thread: impl IntoLuaThread,
args: impl IntoLuaMulti,
) -> LuaResult<ThreadId>
pub fn push_thread_front( &self, thread: impl IntoLuaThread, args: impl IntoLuaMulti, ) -> 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,
args: impl IntoLuaMulti,
) -> LuaResult<ThreadId>
pub fn push_thread_back( &self, thread: impl IntoLuaThread, args: impl IntoLuaMulti, ) -> 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>>
pub fn get_thread_result( &self, id: ThreadId, ) -> Option<LuaResult<LuaMultiValue>>
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.
Sourcepub async fn wait_for_thread(&self, id: ThreadId)
pub async fn wait_for_thread(&self, id: ThreadId)
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Scheduler
impl !RefUnwindSafe for Scheduler
impl !Send for Scheduler
impl !Sync for Scheduler
impl Unpin for Scheduler
impl !UnwindSafe for Scheduler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more