You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I realise it's not a great time to be asking a question give #172...
Is there a way to call a method on a UserData (or indeed a Table, or general Value) from Rust?
My use case is that I want to wrap several Rust types as UserData, which all implement the same trait. In some places I want to be able to pass any of the concrete types from Lua to a Rust function, which only cares that it implements the trait.
In my home-grown bindings I implemented this by adding as_<TRAIT> methods I can call without knowing the type, which returned something like an Rc<RefCell<dyn TRAIT>> which I could then use.
The text was updated successfully, but these errors were encountered:
You can definitely call a method on a table manually by getting the function from the table, then passing the table as the first argument of the function call. It would be nice if there was a better shortcut for doing that.
This is not as easy on userdata, as there's no way currently of calling lua_gettable on a userdata directly, so you can't get a handle to a userdata method directly. However you can always go through Lua to do things like this, so you could just exec lua code to return a handle to that function and then call it like any other function (with the correct AnyUserData as the first argument).
You could make the case that most of the methods from Table should also be available on AnyUserData.
Thanks - I think calling a Lua function will do the trick nicely in this case.
I'd maybe go slightly further than adding most of the Table methods to AnyUserData - Function::call should be available on both (and/or Value) since they can be made callable with the right metamethods.
I realise it's not a great time to be asking a question give #172...
Is there a way to call a method on a
UserData
(or indeed aTable
, or generalValue
) from Rust?My use case is that I want to wrap several Rust types as
UserData
, which all implement the same trait. In some places I want to be able to pass any of the concrete types from Lua to a Rust function, which only cares that it implements the trait.In my home-grown bindings I implemented this by adding
as_<TRAIT>
methods I can call without knowing the type, which returned something like anRc<RefCell<dyn TRAIT>>
which I could then use.The text was updated successfully, but these errors were encountered: