User loaded plugins at runtime

Hello.

Lets say I have a crate that defines a trait SomeTrait, and stores collections of those items in a HashMap<Key, Box< dyn SomeTrait>>. Users of the crate will interact through a looping command line where they can add new predefined types to the HashMap.

What I'd like to learn how to do is have the users be able to create and compile their own libraries with types that impl SomeTrait. I want the user to be able to load those libraries at run time while in the main loop of the crate so that they can load their items into the map. For this use case, I use tui select menus, so I should be able to simply add the name of their type to the select menu and then have logic to add their type to the map when they select their type.

So far I've learned the users will create a dynamically linked library and I will have to use something like the libloading crate for the runtime loading. The users save and load objects via serialization for future use, so I will need typetag and probably erased-serde for their crate. Perhaps I can combine SomeTrait and the typetag attributes and other things into a proc macro for an easier interface for the user?

This is definitely above my skill level. I've been using Rust for about 6 months now but haven't worked with dlls or proc macros yet. I'm mostly just curious if I'm on the right track here, is there anything obvious that I'm missing, and if there's any tools out there for this use case? Any crates with a good example of this workflow?

Thanks.

1 Like