pub struct Scheduler<'lua> { /* private fields */ }
Expand description

A scheduler for running Lua threads and async tasks.

Implementations§

source§

impl<'lua> Scheduler<'lua>

source

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.

source

pub fn status(&self) -> Status

Returns the current status of this scheduler.

source

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.

source

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.

source

pub fn get_exit_code(&self) -> Option<ExitCode>

Gets the exit code for this scheduler, if one has been set.

source

pub fn set_exit_code(&self, code: ExitCode)

Sets the exit code for this scheduler.

This will cause Scheduler::run to exit immediately.

source

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.

source

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.

source

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:

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.

source

pub async fn wait_for_thread(&self, id: ThreadId)

Waits for the LuaThread with the given ThreadId to complete.

This will return instantly if the thread has already completed.

source

pub async fn run(&self)

Runs the scheduler until all Lua threads have completed.

Note that the given Lua state must be the same one that was used to create this scheduler, otherwise this method will panic.

§Panics

Panics if the given Lua state already has a scheduler attached to it.

Trait Implementations§

source§

impl<'lua> Clone for Scheduler<'lua>

source§

fn clone(&self) -> Scheduler<'lua>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Drop for Scheduler<'_>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'lua> !RefUnwindSafe for Scheduler<'lua>

§

impl<'lua> !Send for Scheduler<'lua>

§

impl<'lua> !Sync for Scheduler<'lua>

§

impl<'lua> Unpin for Scheduler<'lua>

§

impl<'lua> !UnwindSafe for Scheduler<'lua>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more