Skip to content

Suggestion: proc_macro for LuaUserDataMethods::add_method #288

@Caellian

Description

@Caellian

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]
impl UserData for MyStruct {
  // ... actual UserData methods ...

  #[rlua::method]
  fn methodA<'lua>(&self, abc: LuaContext<'lua>, number: f32, text: String) {
    // same as: methods.add_method("methodA", |abc, _self, (number, text): (f32, String)| { /*block*/ })
  }

  #[rlua::method]
  fn methodB<'lua>(&mut self, 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions