tomaka / hlua

Rust library to interface with Lua
MIT License
507 stars 48 forks source link

Returning heterogeneous LuaTable from function? #148

Open cjxgm opened 7 years ago

cjxgm commented 7 years ago

I have a function in Rust that will be called in Lua. I'd like the function to return a heterogeneous table like this:

{
    foo = 0,
    bar = "baz",
    [1] = { "hello", "world" },
}

It can be done if there is a way to push an empty table and get both the PushGuard and LuaTable, then I can implement LuaPush for it. But I didn't find a way to do that.

Another way I tried to do it is by implement_lua_push!, which pushes a userdata with a metatable, then set my entries in __index table (like in the example). But then the table can't be iterated over by:

for k, v in pairs(tbl) do
    print(k, v)
end

It will be a bad idea to ask the user to get the __index table for iteration:

for k, v in pairs(getmetatable(tbl).__index) do
    print(k, v)
end