We now have an overview of traits, how to define them and how they work, now it's time to put that knowledge to use and build a plugin system using traits.
A plugin in this system is any type that implements a specific trait. Each plugin will perform a specific task, and the system should manage a collection of these plugins, executing them in sequence. You’ll also address advanced issues like object safety and resolving potential conflicts between overlapping trait implementations.
Your Task
Design and implement a plugin system using trait objects. You will:
- Define a
Plugintrait that includes methods for initialization and execution. - Create a
PluginManagerstruct to manage plugins. It should:- Dynamically load plugins implementing the
Plugintrait. - Allow adding and removing plugins.
- Execute all registered plugins in sequence.
- Dynamically load plugins implementing the
Requirements
- The
Plugintrait should include the following methods:fn name(&self) -> &str;- Returns the name of the plugin.fn execute(&self);- Executes the plugin's functionality.
- The
PluginManagershould:- Have the following methods and associated functions:
new() -> Self- Creates a newPluginManagerinstance.add_plugin- Adds a plugin to the list.remove_plugin- Removes a plugin from the list and returns the removed plugin if found.execute_all- Executes all registered plugins.
- Have the following methods and associated functions:
- If a duplicate plugin is added (with the same name), it should panic with the message "Plugin with name [name] already exists".
Make sure you make all relevant items public.
Hints
<details> <summary>Click here to reveal hints</summary>- Dynamic Dispatch: Store plugins in a
Vec<Box<dyn Plugin>>for dynamic dispatch. - Plugin Uniqueness: Use the
namemethod to identify and ensure uniqueness among plugins.
← PreviousNext →
pub trait Plugin {// 1. Finish the trait}pub struct PluginManager {// 2. Finish the struct// Make fields public}// 3. Implement the PluginManagerimpl PluginManager {}// Example usagepub fn main() {let mut manager = PluginManager::new();manager.add_plugin(Box::new(MyPlugin::new()));manager.execute_all();}