Expand description
Lua 5.4 ported to Rust.
§Quickstart
use tsuki::builtin::{BaseLib, CoroLib, MathLib, StringLib, TableLib, Utf8Lib};
use tsuki::{Args, Context, Lua, Ret, Value, fp};
fn main() {
// Set up.
let lua = Lua::new(());
lua.use_module(None, true, BaseLib).unwrap();
lua.use_module(None, true, CoroLib).unwrap();
lua.use_module(None, true, MathLib).unwrap();
lua.use_module(None, true, StringLib).unwrap();
lua.use_module(None, true, TableLib).unwrap();
lua.use_module(None, true, Utf8Lib).unwrap();
lua.global().set_str_key("myfunc", fp!(myfunc));
// Run on main thread.
let chunk = lua.load("abc.lua", "return myfunc()").unwrap();
let result = lua.call::<Value<_>>(chunk, ()).unwrap();
match result {
Value::Str(v) => assert_eq!(v.as_str(), Some("Hello world!")),
_ => todo!(),
}
}
fn myfunc(cx: Context<(), Args>) -> Result<Context<(), Ret>, Box<dyn core::error::Error>> {
cx.push_str("Hello world!")?;
Ok(cx.into())
}§Types that can be converted to UnsafeValue
You can pass the value of the following types for impl Into<UnsafeValue>:
- Nil
- bool
- Fp
- AsyncFp
- i8
- i16
- i32
- i64
- u8
- u16
- u32
- f32
- f64
- Number
- Reference to Str
- Reference to Table
- Reference to LuaFn
- Reference to UserData
- Reference to Thread
- Ref
- Value
- Arg or a reference to it
The value will be converted to corresponding Lua value. Tsuki does not expose [UnsafeValue] by design so you cannot construct its value. Tsuki also never handout the value of [UnsafeValue].
§Store value in registry
You need to create a type per key in registry:
use tsuki::{RegKey, Table};
struct MyKey;
impl<A> RegKey<A> for MyKey {
type Value<'a>
= Table<A>
where
A: 'a;
}Type type itself is a key, not its value. Then you can use Lua::set_registry() or Context::set_registry() to set the value and Lua::registry() or Context::registry() to retrieve the value:
Modules§
- builtin
- Implementation of Lua standard libraries.
Macros§
Structs§
- Arg
- Argument passed from Lua to Rust function.
- ArgNot
Found - Represents an error when
Argdoes not exists. - Args
- Call arguments encapsulated in
Context. - AsyncFp
- Asynchronous Rust function.
- Call
Error - Represents an error when
FporAsyncFpreturn an error. - Chunk
Info - Contains information for Lua chunk.
- Context
- Context to invoke Fp and AsyncFp.
- Dynamic
Inputs - Implementation of [
Inputs] which size does not known at compile time. - Fp
- Non-Yieldable Rust function.
- Lua
- Global states shared with all Lua threads.
- LuaFn
- Lua function.
- Module
Exists - Represents an error when Lua::use_module() fails due to the module already exists.
- Nil
- Unit struct to create
nilvalue. - Recursive
Call - Represents an error when a function that cannot be recursive call itself either directly or indirectly.
- Ref
- Strong reference to Lua object.
- Ret
- Call results encapsulated in
Context; - Stack
Overflow - Represents an error when Lua stack is overflow.
- Str
- Lua string.
- Table
- Lua table.
- Thread
- Lua thread (AKA coroutine).
- User
Data - Lua full userdata.
- YieldFp
Enums§
- Arith
Error - Represents an error when arithmetic operation fails.
- Metatable
Error - Error when attempt to set an invalid metatable.
- Number
- Helper enum to encapsulates either integer or float.
- Ops
- Type of operator.
- Parse
Error - Represents an error when failed to parse Lua source.
- Table
Error - Represents an error when the operation on a table fails.
- TryCall
- Success result of
Context::try_forward(). - Type
- Type of Lua value.
- Value
- Lua value.