dyn-inventory
proc macro for building runtime plugin registries using dyn-compatible traits and the inventory crate.
this crate generates code to:
- register plugins that implement a trait object (
dyn trait) - carry typed metadata alongside each plugin
- collect and instantiate all registered plugins at runtime
use dyn_inventory;
dyn_inventory!
Why dyn-compatible traits
the plugins produced by this crate are stored and used as Box<dyn Trait>. when used with inventory, this allows for new plugin registries to be developed for decentralized libraries and frameworks.
Quick Start
- add dependencies:
[]
= "0.3"
= "0.2"
- define a trait that is dyn-compatible:
- declare your inventory using the
dyn_inventory!proc macro:
dyn_inventory!;
[!TIP] what this generates:
- a struct
GreeterPluginwith the fields you declared, and aBox<dyn Greeter>- an inventory registration type
inventory::collect!(GreeterPluginInit)- a collector
GreeterPluginCollectorthat haspluginof typeVec<GreeterPlugin>
- register a plugin somewhere in your code (could be another crate that depends on your trait crate):
use crate::;
use emit;
// this expands to a unit struct named `MyGreeter` and registers it into the inventory
emit!
// you implement the trait for the generated unit struct
- collect your plugins at runtime:
let collected = new;
for plugin in &collected.plugins
Macro Syntax
use dyn_inventory;
dyn_inventory!;
Extra Parameters
two extra params are currently accepted:
init_name = ident- sets the name of the generated initialization struct. by default it is the snake_case of
StructName(for example,GreeterPlugin->greeter_plugin).
- sets the name of the generated initialization struct. by default it is the snake_case of
Advanced: customizing collection
the collector type is named by appending Collector to your struct name. it exposes:
new()-> builds the collection without modificationnew_with(|item: &mut StructName| {...})-> allows you to mutate the raw entries after they are instantiated intoBox<dyn TraitName>
Constraints
- your trait must be object-safe (dyn-compatible)
- the
inventorycrate must be linked into the final binary; ensure your plugin crates depend oninventoryand your main binary pulls in the crates that perform registrations - plugins must not carry state. instead, pass state as trait function parameters.