enum_delegate: Easily replace dynamic dispatch with an enum, for speed and serialization
TLDR: If you know enum_dispatch, this is very similar, except the macros work across crates, and there is some support for associated types.
Quick Walk-through
Add enum_delegate to each crate you plan to use it in:
enum_delegate = "0.1"
Annotate your trait with #[enum_delegate::register]:
#[enum_delegate::register]
trait SayHello {
fn say_hello(&self, name: &str) -> String;
}
Create an enum with variants that all look like VariantName(VariantType). Each variant field must implement your trait. Now, annotate the enum with #[enum_delegate::implement(trait name)].
struct Arthur;
impl SayHello for Arthur {...}
struct Pablo;
impl SayHello for Pablo {...}
#[enum_delegate::implement(SayHello)]
enum People {
Arthur(Arthur),
Pablo(Pablo),
}
Your trait will be implemented for the enum, and conversions such as From<Arthur> and TryInto<Arthur> will also be generated. You can now use it similarly to a Box<dyn SayHello>, but it will be faster!
More Info
There is a more complete guide in the Readme. Also a few examples and tests in the repo.
For the macromancers among you, I also wrote an overview of what's going on behind the scenes: How does it work?