• 0 Posts
  • 1 Comment
Joined 2 years ago
cake
Cake day: June 22nd, 2023

help-circle
  • Getting metadata like this requires adding an extension to the language, either directly in code (as with a macro) or by messing with the build process (running another pass through your code to extract the metadata).

    If you’re not adverse to using a global, you could write a macro that you use instead of impl for these traits. So, for example:

    register_plugin!(impl MyTrait for MyStruct {
      /* regular impl stuff goes here */
    });
    

    This macro would populate the global with metadata about the association between MyStruct and MyTrait. You could then pass a copy of it to AllFunctions::new.

    Alternatively, write a macro that just does the registration, which is like what you’re already doing:

    impl MyTrait for MyStruct { ... }
    
    register_plugin!(MyTrait, MyStruct);
    

    Another option would be to write something funky in build.rs that does whatever rustdoc does to discover impls and pass that to the rest of your code as const data. As a hack, you could have it invoke rustdoc and then parse the output.

    How dynamic is this plugin system, though? If you can only change the registered plugins by rebuilding, then automatic discovery doesn’t seem so warranted. Invoking a simple registration macro at the bottom of each file would save a lot of complexity budget.