Crate tsuki

Crate tsuki 

Source
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>:

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§

fp
Helper macro to construct Fp or AsyncFp.

Structs§

Arg
Argument passed from Lua to Rust function.
ArgNotFound
Represents an error when Arg does not exists.
Args
Call arguments encapsulated in Context.
AsyncFp
Asynchronous Rust function.
CallError
Represents an error when Fp or AsyncFp return an error.
ChunkInfo
Contains information for Lua chunk.
Context
Context to invoke Fp and AsyncFp.
DynamicInputs
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.
ModuleExists
Represents an error when Lua::use_module() fails due to the module already exists.
Nil
Unit struct to create nil value.
RecursiveCall
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;
StackOverflow
Represents an error when Lua stack is overflow.
Str
Lua string.
Table
Lua table.
Thread
Lua thread (AKA coroutine).
UserData
Lua full userdata.
YieldFp

Enums§

ArithError
Represents an error when arithmetic operation fails.
MetatableError
Error when attempt to set an invalid metatable.
Number
Helper enum to encapsulates either integer or float.
Ops
Type of operator.
ParseError
Represents an error when failed to parse Lua source.
TableError
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.

Traits§

Module
Provides interface for Rust to create a Lua module.
RegKey
Key to store value on Lua registry.