pub struct Thread(/* private fields */);
Expand description
Handle to an internal Lua thread (coroutine).
Implementations§
Source§implThread
implThread
Sourcepub fnresume<R>(&self, args: implIntoLuaMulti) ->Result<R>where R:FromLuaMulti,
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 threadreturn
s 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),}
Sourcepub fnstatus(&self) ->ThreadStatus
pub fnstatus(&self) ->ThreadStatus
Gets the status of the thread.
Sourcepub fnset_hook<F>(&self, triggers:HookTriggers, callback: F) ->Result<()>
Available onnon-crate featureluau
only.
pub fnset_hook<F>(&self, triggers:HookTriggers, callback: F) ->Result<()>
luau
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
.
Sourcepub fnremove_hook(&self)
Available onnon-crate featureluau
only.
pub fnremove_hook(&self)
luau
only.Removes any hook function from this thread.
Sourcepub fnreset(&self, func:Function) ->Result<()>
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.
Sourcepub fninto_async<R>(self, args: implIntoLuaMulti) ->Result<AsyncThread<R>>where R:FromLuaMulti,
Available oncrate featureasync
only.
pub fninto_async<R>(self, args: implIntoLuaMulti) ->Result<AsyncThread<R>>where R:FromLuaMulti,
async
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.yield
values 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);
Sourcepub fnsandbox(&self) ->Result<()>
Available oncrate featureluau
only.
pub fnsandbox(&self) ->Result<()>
luau
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);
Sourcepub fnto_pointer(&self) ->*constc_void
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§
implSend forThread
implSync forThread
Auto Trait Implementations§
Blanket Implementations§
Source§impl<T>BorrowMut<T> for Twhere T: ?Sized,
impl<T>BorrowMut<T> for Twhere T: ?Sized,
Source§fnborrow_mut(&mut self) ->&mut T
fnborrow_mut(&mut self) ->&mut T
Source§impl<T>CloneToUninit for Twhere T:Clone,
impl<T>CloneToUninit for Twhere T:Clone,
Source§impl<T>FromLuaMulti for Twhere T:FromLua,
impl<T>FromLuaMulti for Twhere T:FromLua,
Source§fnfrom_lua_multi(values:MultiValue, lua: &Lua) ->Result<T,Error>
fnfrom_lua_multi(values:MultiValue, lua: &Lua) ->Result<T,Error>
fnfrom_lua_args( args:MultiValue, i:usize, to:Option<&str>, lua: &Lua,) ->Result<T,Error>
unsafe fnfrom_stack_multi(nvals:i32, lua: &RawLua) ->Result<T,Error>
unsafe fnfrom_stack_args( nargs:i32, i:usize, to:Option<&str>, lua: &RawLua,) ->Result<T,Error>
Source§impl<T>IntoEither for T
impl<T>IntoEither for T
Source§fninto_either(self, into_left:bool) ->Either<Self, Self>ⓘ
fninto_either(self, into_left:bool) ->Either<Self, Self>ⓘ
self
into aLeft
variant ofEither<Self, Self>
ifinto_left
istrue
.Convertsself
into aRight
variant ofEither<Self, Self>
otherwise.Read moreSource§fninto_either_with<F>(self, into_left: F) ->Either<Self, Self>ⓘ
fninto_either_with<F>(self, into_left: F) ->Either<Self, Self>ⓘ
self
into aLeft
variant ofEither<Self, Self>
ifinto_left(&self)
returnstrue
.Convertsself
into aRight
variant ofEither<Self, Self>
otherwise.Read more