Skip Navigation
Jump
[Help] Is there a way to detect all structs in the current crate that implement a certain trait?
  • 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.

    1
  • Jump
    Welcome, please introduce yourself!
  • Funny coincidence -- my wife has just gotten me to start following Legend of the Galactic Heroes. I'm reading the light novels (not much time for speed-running 45 hours of anime), and I also picked up the Vampire Hunter D novels.

    She's said that there are a few active Discord communities for Legend of the Galactic Heroes. Are you aware of anything on the Fediverse?

    1