Reputation: 983
I've been working on re-developing a project of mine in Rust to try it out, and one of the things I'd like to do is a plugin system. I haven't looked much into dylib yet, but for now I'd like to get the architecture right so that adding dylib later wouldn't be too hard.
My question is about plugins config, I'd like to call for each plugin an init function, and allow them to return an object that would then be passed to every call of their functions. I don't really care about what is in that object; that's the plugin's business.
I tried using a Box<dyn>
but of course it doesn't work without a trait. Does that mean the only way would be to declare an empty trait, like PluginConfig
for example, and use dyn with that? Then the plugins could just implement that trait and return that from their init. Or am I missing the correct way to do it completely ? If I understood the book correctly, doing that would make it impossible for the plugin later on to access their fields, since they aren't defined in the Trait, and thus would not work properly.
tl;dr I'm just looking for the rust equivalent of storing a void *
.
Upvotes: 1
Views: 796
Reputation: 71899
The Rust method for this is Box<dyn Any>
, which the plugin can then safely cast back to its own type.
But you should be aware that Rust has no stable ABI, so if you have anything in your plugin interface which isn't just C-compatible, your plugins and your main program have to be compiled with the same compiler version and options in order to safely work.
Upvotes: 8