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
{{ message }}
This repository was archived by the owner on Sep 12, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 12, 2025. It is now read-only.
Suggestion: proc_macro for LuaUserDataMethods::add_method #288
It would be really nice if rlua provided a macro that allowed declaring methods via add_method and add_method_mut on UserData in a simpler way.
Something like:
#[rlua::proc_methods]implUserDataforMyStruct{// ... actual UserData methods ...#[rlua::method]fnmethodA<'lua>(&self,abc:LuaContext<'lua>,number:f32,text:String){// same as: methods.add_method("methodA", |abc, _self, (number, text): (f32, String)| { /*block*/ })}#[rlua::method]fnmethodB<'lua>(&mutself,cde:LuaContext<'lua>,other_number:f32,some_text:String){// same as: methods.add_method_mut("methodB", |cde, _self, (other_number, some_text): (f32, String)| { /*block*/ })}}
This is quite involved to implement. Note that _self handling requires modification of the inner code block, but syn has a visit-mut feature for precisely this functionality.
Benefits
Resulting API would be 10x nicer looking.
Types could be checked using generated glue that throws very specific error messages containing exact names of parameters which failed.
Alternative proposal
A normal macro that wraps individual add_method calls would still allow generating more specific error messages while avoiding separation of argument names and types in callback signature.
would require some minor tweaking to wrap add_method(_mut) but it's a good example.
This would probably have to be tuned to allow FromLuaMulti as in #287 for the last argument. But I'll try to mix this macro into that PR to provide much better error messages.
The issue with that implementation is that it doesn't handle complex types well (e.g. OneOf<String, LuaTable>).
Activity
jugglerchris commentedon Dec 2, 2023
That does look like it'd make lots of bindings much nicer!
Caellian commentedon Dec 3, 2023
For reference, I wrote a macro for registering global constructor functions for my bindings which basically does the alternative proposal:
would require some minor tweaking to wrap
add_method(_mut)but it's a good example.This would probably have to be tuned to allow
FromLuaMultias in #287 for the last argument. But I'll try to mix this macro into that PR to provide much better error messages.The issue with that implementation is that it doesn't handle complex types well (e.g.
OneOf<String, LuaTable>).jugglerchris commentedon Dec 3, 2023
FWIW in one of my projects, I use a (declarative) macro which looks like:
where the
server_redirectetc. are the rlua-style functions implementing the method.I also support traits:
From Lua a
Type1userdata then has both its own methods and the trait methods.