Movatterモバイル変換


[0]ホーム

URL:


Docs.rs

StructThread

Source
pub struct Thread(/* private fields */);
Expand description

Handle to an internal Lua thread (coroutine).

Implementations§

Source§

implThread

Source

pub fnresume<R>(&self, args: implIntoLuaMulti) ->Result<R>
where R:FromLuaMulti,

Resumes execution of this thread.

Equivalent tocoroutine.resume.

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

If the thread is no longer resumable (meaning it has finished execution or encountered anerror), this will returnError::CoroutineUnresumable, otherwise will returnOk asfollows:

If the thread callscoroutine.yield, returns the values passed toyield. If the threadreturns values from its main function, returns those.

§Examples
letthread: 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 failmatchthread.resume::<u32>(()) {Err(Error::CoroutineUnresumable) => {},    unexpected =>panic!("unexpected result {:?}", unexpected),}
Source

pub fnstatus(&self) ->ThreadStatus

Gets the status of the thread.

Source

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

Available onnon-crate featureluau only.

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

This function is similar orLua::set_hook except that it sets for the thread.You can have multiple hooks for different threads.

To remove a hook callThread::remove_hook.

Source

pub fnremove_hook(&self)

Available onnon-crate featureluau only.

Removes any hook function from this thread.

Source

pub fnreset(&self, func:Function) ->Result<()>

Resets a thread

InLua 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 errorsin 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 fninto_async<R>(self, args: implIntoLuaMulti) ->Result<AsyncThread<R>>
where R:FromLuaMulti,

Available oncrate featureasync only.

ConvertsThread to anAsyncThread which implementsFuture andStream traits.

Only resumable threads can be converted toAsyncThread.

args are pushed to the thread stack and will be used when the thread is resumed.The object callsresume while polling and also allow to run Rust futuresto completion using an executor.

UsingAsyncThread as aStream allow to iterate throughcoroutine.yieldvalues whereasFuture version discards that values and poll until the finalone (returned from the thread function).

§Examples
usefutures_util::stream::TryStreamExt;letthread: Thread = lua.load(r#"    coroutine.create(function (sum)        for i = 1,10 do            sum = sum + i            coroutine.yield(sum)        end        return sum    end)"#).eval()?;letmutstream = thread.into_async::<i64>(1)?;letmutsum =0;while letSome(n) = stream.try_next().await?{    sum += n;}assert_eq!(sum,286);
Source

pub fnsandbox(&self) ->Result<()>

Available oncrate featureluau only.

Enables sandbox mode on this thread.

Under the hood replaces the global environment table with a new table,that performs writes locally and proxies reads to caller’s global environment.

This mode ideally should be used together with the global sandbox mode [Lua::sandbox].

Please note that Luau links environment table with chunk when loading it into Lua state.Therefore you need to load chunks into a thread to link with the thread environment.

§Examples
letlua = Lua::new();letthread = lua.create_thread(lua.create_function(|lua2, ()| {    lua2.load("var = 123").exec()?;assert_eq!(lua2.globals().get::<u32>("var")?,123);Ok(())})?)?;thread.sandbox()?;thread.resume::<()>(())?;// The global environment should be unchangedassert_eq!(lua.globals().get::<Option<u32>>("var")?,None);
Source

pub fnto_pointer(&self) ->*constc_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§

implClone forThread

Source§

fnclone(&self) ->Thread

Returns a duplicate of the value.Read more
1.0.0 ·Source§

fnclone_from(&mut self, source: &Self)

Performs copy-assignment fromsource.Read more
Source§

implDebug forThread

Source§

fnfmt(&self, fmt: &mutFormatter<'_>) ->Result

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

implFromLua forThread

Source§

fnfrom_lua(value:Value, _: &Lua) ->Result<Thread>

Performs the conversion.
Source§

implIntoLua for &Thread

Source§

fninto_lua(self, _: &Lua) ->Result<Value>

Performs the conversion.
Source§

implIntoLua forThread

Source§

fninto_lua(self, _: &Lua) ->Result<Value>

Performs the conversion.
Source§

implPartialEq forThread

Source§

fneq(&self, other: &Self) ->bool

Tests forself andother values to be equal, and is used by==.
1.0.0 ·Source§

fnne(&self, other:&Rhs) ->bool

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

implSend forThread

Source§

implSync forThread

Auto Trait Implementations§

§

implFreeze forThread

§

impl !RefUnwindSafe forThread

§

implUnpin forThread

§

impl !UnwindSafe forThread

Blanket Implementations§

Source§

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

Source§

fntype_id(&self) ->TypeId

Gets theTypeId ofself.Read more
Source§

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

Source§

fnborrow(&self) ->&T

Immutably borrows from an owned value.Read more
Source§

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

Source§

fnborrow_mut(&mut self) ->&mut T

Mutably borrows from an owned value.Read more
Source§

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

Source§

unsafe fnclone_to_uninit(&self, dest:*mutu8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment fromself todest.Read more
Source§

impl<T>From<T> for T

Source§

fnfrom(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

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

Performs the conversion.Read more
Source§

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

Source§

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

Source§

unsafe fnfrom_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§

fninto(self) -> U

CallsU::from(self).

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

Source§

impl<T>IntoEither for T

Source§

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

Convertsself into aLeft variant ofEither<Self, Self>ifinto_left istrue.Convertsself into aRight variant ofEither<Self, Self>otherwise.Read more
Source§

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

Convertsself into aLeft variant ofEither<Self, Self>ifinto_left(&self) returnstrue.Convertsself into aRight variant ofEither<Self, Self>otherwise.Read more
Source§

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

Source§

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

Performs the conversion.
Source§

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

Source§

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

Source§

typeOwned = T

The resulting type after obtaining ownership.
Source§

fnto_owned(&self) -> T

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

fnclone_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§

typeError =Infallible

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

fntry_from(value: U) ->Result<T, <T asTryFrom<U>>::Error>

Performs the conversion.
Source§

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

Source§

typeError = <U asTryFrom<T>>::Error

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

fntry_into(self) ->Result<U, <U asTryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T>MaybeSend for T
where T:Send,


[8]ページ先頭

©2009-2025 Movatter.jp