Search
![rust](https://programming.dev/pictrs/image/1f7e5534-88d3-4d6f-bc57-7cda9553890f.webp?format=webp&thumbnail=64)
Is there a way to detect all structs in the current crate that implement a certain trait?
Hi all.
I want to develop a plugin system within my program, and I have a trait that functions defined by plugins should implement.
Currently, my code gets all the functions in a HashMap and then calls them by their name. Problem is, I have to create that hashmap myself by inserting every function myself.
I would really appreciate it if there was a way to say, suppose, all pub members of mod functions::
that implement this trait PluginFunction
call register(hashmap)
function. So as I add more functions as mod
in functions
it'll be automatically added on compile.
Pseudocode:
Files:
undefined
src/ ├── attrs.rs ├── functions │ ├── attrs.rs │ ├── export.rs │ └── render.rs ├── functions.rs ├── lib.rs
Basically, in mod functions
I want:
undefined
impl AllFunctions{ pub fn new() -> Self { let mut functions_map = HashMap::new();[[ register_all!(crate::functions::* implementing PluginFunction, &mut functions_map); Self { function_map } } }
Right