1

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 *.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Ulrar
  • 895
  • 8
  • 17
  • Your question seems to come down to how to do reflection in Rust. Relevant questions: [1](https://stackoverflow.com/q/30486227/1233251) [2](https://stackoverflow.com/q/20445975/1233251) You most likely do need to specify a trait for your plugin types. And in order to be usable, the trait would need to have the necessary methods to gain access to that plugin's features. – E_net4 Apr 24 '19 at 11:05

1 Answers1

8

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.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • If you want to be ABI compatible, you can define an unsafe C style ABI, with the object being `*const c_void`. – rodrigo Apr 24 '19 at 11:58
  • That's completely fine for now, I'll just avoid dylib and compile the "plugins" directly into the main project for now, I just want to have the option to make it into actual plugins later if it ends up making sense. Thanks ! – Ulrar Apr 24 '19 at 12:22