Static Dispatch
Derive a trait for an enum, where all variants implement the trait.
;
;
async
Overview
- Annotate the trait with
static_dispatch::dispatch
- This generates a macro with
macro_rules
- Use
static_dispatch::dispatch(macro_export)
to export the macro
- This generates a macro with
- Annotate the struct with
static_dispatch::dispatch(<TraitName>)
- This invokes the macro to generate the trait implementation
- Use
static_dispatch::dispatch(<crate>::<TraitName>)
for use withmacro_export
Supported
- Async methods
- Trait and enum in different modules or crates
- Trait with generics, lifetimes or const generics.
no_std
support- Implement trait with multiple enums
- Implement multiple traits for an enum
Not Supported
- Use a type alias for the trait
- Concrete implementation for a generic trait
- Traits with items, which are not function with
self
,&self
or&mut self
impl
in return position
Comparison to enum_dispatch
- This crate only generates the trait implementation for the enum, nothing more
- Data between the trait and the enum is passed with a
macro_rule
, not as a side effect in memory