Hi everyone!
In our current project, we have "plugins", which need some initialization and offer some methods later on.
Currently we're doing this as follows:
All plugins implement two traits, InstanceInit
and InstanceFeatures
.
InstanceInit offers two methods
get_default_parameters() -> Vec<Parameter>
initialize(parameters: Vec<Parameter>) -> Result<Box<dyn InstanceFeatures>, Box<dyn Error>>
InstanceFeatures then offers a few methods that all contain &self
.
In Rust, I can easily make a trait object out of InstanceFeatures
, but not out of InstanceInit
.
In our code, we need to keep track of an assortment of different plugins and then instantiate them at runtime, depending on user input.
Currently we're doing that by requiring that all plugins implement both traits and manually unpacking the init-trait into a struct of
PluginInit{
name: String,
get_default_parameters: Box<fn() -> Vec<Parameters>>,
initialize: Box<fn(Vec<Parameter>)-> Result<Box<dyn InstanceFeatures>, Box<dyn Error>>
}
Is there a more idiomatic way to do this?
I understand that I can't create a trait object by just doing Box::new(Plugin)
for a struct Plugin
that implements InstanceInit
and InstanceFeatures
, but the current way we're doing this seems a bit convoluted.
Thanks in advance!