#derive #table #rlua #lua #traits #macro #from-lua-table

macro rlua-table-derive

This crate provides a procedural macro to turn lua tables into rust types via rlua

1 unstable release

Uses old Rust 2015

0.1.0 Jan 3, 2018

#9 in #rlua

MIT license

5KB
63 lines

rlua-table-derive

This crate provides a custom derive for a FromLuaTable trait

To derive just add FromLuaTable to the derive list for a struct. Default must be implemented for a type to use this macro.

An example usage can be found in the examples directory. I have copied it here for convenience:

#[macro_use] extern crate rlua_table_derive;
extern crate rlua;


#[derive(Default, Debug, Clone, FromLuaTable)]
pub struct Thing{
    x: f32,
    y: f32,

    name: String,
}

trait FromLuaTable {
    fn from_lua_table(table: &rlua::Table) -> Self;
}

const LUA_STRING: &str = "
thing = {
    x=2,
    name='john',
}
";

fn main() {
    let lua = rlua::Lua::new();
    lua.eval::<()>(LUA_STRING, Some("baked in")).unwrap();

    let default = Thing::default();

    let table = lua.globals().get("thing").unwrap();
    let from_lua = Thing::from_lua_table(&table);

    print!("Default: {:?}\n", default);
    print!("From lua: {:?}\n", from_lua);
}

Dependencies

~2MB
~44K SLoC