Skip to main content

Module thread

Module thread 

Source
Expand description

Lua thread (coroutine) handling.

This module provides types for creating and working with Lua coroutines from Rust. Coroutines allow cooperative multitasking within a single Lua state by suspending and resuming execution at well-defined yield points.

§Basic Usage

Threads are created via Lua::create_thread and driven by calling Thread::resume:

let lua = Lua::new();
let thread: Thread = lua.load(r#"
    coroutine.create(function(a, b)
        coroutine.yield(a + b)
        return a * b
    end)
"#).eval()?;

assert_eq!(thread.resume::<i32>((3, 4))?, 7);
assert_eq!(thread.resume::<i32>(())?,    12);

§Async Support

When the async feature is enabled, a Thread can be converted into an AsyncThread via Thread::into_async, which implements both Future and Stream. This integrates Lua coroutines naturally with Rust async runtimes such as Tokio.

Structs§

AsyncThreadasync
Thread (coroutine) representation as an async Future or Stream.
Thread
Handle to an internal Lua thread (coroutine).

Enums§

ThreadStatus
Status of a Lua thread (coroutine).