Struct Thread

Source
pub struct Thread(/* private fields */);
Available on crate feature mlua only.
Expand description

Handle to an internal Lua thread (coroutine).

Implementations§

Source§

impl Thread

Source

pub fn resume<R>(&self, args: impl IntoLuaMulti) -> Result<R, Error>
where R: FromLuaMulti,

Resumes execution of this thread.

Equivalent to coroutine.resume.

Passes args as arguments to the thread. If the coroutine has called coroutine.yield, it will return these arguments. Otherwise, the coroutine wasn’t yet started, so the arguments are passed to its main function.

If the thread is no longer resumable (meaning it has finished execution or encountered an error), this will return Error::CoroutineUnresumable, otherwise will return Ok as follows:

If the thread calls coroutine.yield, returns the values passed to yield. If the thread returns values from its main function, returns those.

§Examples
let thread: Thread = lua.load(r#"
    coroutine.create(function(arg)
        assert(arg == 42)
        local yieldarg = coroutine.yield(123)
        assert(yieldarg == 43)
        return 987
    end)
"#).eval()?;

assert_eq!(thread.resume::<u32>(42)?, 123);
assert_eq!(thread.resume::<u32>(43)?, 987);

// The coroutine has now returned, so `resume` will fail
match thread.resume::<u32>(()) {
    Err(Error::CoroutineUnresumable) => {},
    unexpected => panic!("unexpected result {:?}", unexpected),
}
Source

pub fn status(&self) -> ThreadStatus

Gets the status of the thread.

Source

pub fn set_hook<F>(&self, triggers: HookTriggers, callback: F)
where F: Fn(&Lua, Debug<'_>) -> Result<VmState, Error> + MaybeSend + 'static,

Sets a hook function that will periodically be called as Lua code executes.

This function is similar or [Lua::set_hook] except that it sets for the thread. To remove a hook call [Lua::remove_hook].

Source

pub fn reset(&self, func: Function) -> Result<(), Error>

Resets a thread

In Lua 5.4: cleans its call stack and closes all pending to-be-closed variables. Returns a error in case of either the original error that stopped the thread or errors in closing methods.

In Luau: resets to the initial state of a newly created Lua thread. Lua threads in arbitrary states (like yielded or errored) can be reset properly.

Other Lua versions can reset only new or finished threads.

Sets a Lua function for the thread afterwards.

Source

pub fn to_pointer(&self) -> *const c_void

Converts this thread to a generic C pointer.

There is no way to convert the pointer back to its original value.

Typically this function is used only for hashing and debug information.

Trait Implementations§

Source§

impl Clone for Thread

Source§

fn clone(&self) -> Thread

Returns a duplicate 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 Debug for Thread

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl FromLua for Thread

Source§

fn from_lua(value: Value, _: &Lua) -> Result<Thread, Error>

Performs the conversion.
Source§

impl IntoLua for &Thread

Source§

fn into_lua(self, _: &Lua) -> Result<Value, Error>

Performs the conversion.
Source§

impl IntoLua for Thread

Source§

fn into_lua(self, _: &Lua) -> Result<Value, Error>

Performs the conversion.
Source§

impl PartialEq for Thread

Source§

fn eq(&self, other: &Thread) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl Freeze for Thread

§

impl !RefUnwindSafe for Thread

§

impl !Send for Thread

§

impl !Sync for Thread

§

impl Unpin for Thread

§

impl !UnwindSafe for Thread

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromLuaMulti for T
where T: FromLua,

Source§

fn from_lua_multi(values: MultiValue, lua: &Lua) -> Result<T, Error>

Performs the conversion. Read more
Source§

fn from_lua_args( args: MultiValue, i: usize, to: Option<&str>, lua: &Lua, ) -> Result<T, Error>

Source§

unsafe fn from_stack_multi(nvals: i32, lua: &RawLua) -> Result<T, Error>

Source§

unsafe fn from_stack_args( nargs: i32, i: usize, to: Option<&str>, lua: &RawLua, ) -> Result<T, Error>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> IntoLuaMulti for T
where T: IntoLua,

Source§

fn into_lua_multi(self, lua: &Lua) -> Result<MultiValue, Error>

Performs the conversion.
Source§

unsafe fn push_into_stack_multi(self, lua: &RawLua) -> Result<i32, Error>

Source§

impl<T> IntoResult<T> for T

Source§

type Error = Infallible

The error type in the returned Result.
Source§

fn into_result(self) -> Result<T, <T as IntoResult<T>>::Error>

Converts the value into a Result.
Source§

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

Source§

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>,

Source§

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>,

Source§

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> MaybeSend for T