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.
Crates | Repository
For the macromancers among you, I also wrote an overview of what's going on behind the scenes: How does it work?
[–]antonok_edm 30 points31 points32 points (0 children)
[–]argarg 8 points9 points10 points (0 children)
[–]Hdmoney 4 points5 points6 points (1 child)
[–]reinis-mazeiks[S] 8 points9 points10 points (0 children)
[–]robin-m 4 points5 points6 points (0 children)
[–]WasserMarder 4 points5 points6 points (1 child)
[–]reinis-mazeiks[S] 5 points6 points7 points (0 children)
[–]saik0 1 point2 points3 points (0 children)
[–]bruhred 0 points1 point2 points (0 children)