This crate provides a procedural macro, specialized_dispatch, a convenient
way to implement different behaviors based on type of an expression.
This works by creating different specializations in the callsite by making use
of min_specialization nightly feature under the hood.
As such, the caller needs to enable this nightly feature for the library from which this macro is called.
Simple Example
use specialized_dispatch;
example function roughly expands to below code. Note that exact expansion is
internal implementation detail. This example is provided to demonstrate how it
works under the hood.
The example above is included in the repository.
It can be run with cargo run --example simple_example.
Expanded code can be inspected using cargo-expand: cargo expand --example simple_example.
Trait Bounds
Trait bounds can be provided for the default case:
use Display;
use specialized_dispatch;
// The expression type must also bind to the same trait.
Likewise, the example above is included in the repository.
It can be run with cargo run --example trait_bound or inspected with
cargo-expand.
Passing Extra Arguments
Extra arguments can be passed to specializations. Argument types need to declared explicitly (i.e. they won't be captured automatically as it happens with closures).
use Display;
use specialized_dispatch;
Specialization still happens based on the first argument only.
As with previous examples, the example above is included in the
repository as well. It can be run with cargo run --example pass_args or
inspected with cargo-expand.
Advanced Serdelike Example
Let's say you are implementing a deserializer. There might be certain types
that work well with your own deserializer, while they have a default
implementation for generic deserializers (or even unimplemented! by default).
To simplify the example, we will create a water-down version of relevant
serde traits.
use specialized_dispatch;
/// A simplified version of `serde::de::Deserializer`.
/// A simplified version of `serde::de::Deserialize`.
The example above is included in the repository. It can be
run with cargo run --example serdelike_example or inspected with
cargo-expand.
Limitations
Requires nightly
This is due to relying on min_specialization feature.
Only concrete types are supported for specialization
Specialization can be used only with concrete types (e.g. subtraits cannot be
used for specialization). This is an existing limitation inherited from the
current implementation of min_specialization feature.
Variables aren't captured automatically
The macro expands its arms to some method implementations. As such, it cannot refer to other variables in the scope where it's called from.
However, extra arguments can be passed when they are explicitly declared in the macro. Please refer to Passing Extra Arguments section.
Not working well with lifetimes
I tried implementing lifetime support in various places, but I hit some compiler errors and in some cases Internal Compiler Errors (ICE). See TODO in Advanced Serdelike Example.
This is very likely due to underlying min_specialization implementation not
being very mature yet, though it's quite possible I botched something somewhere
(Please file an issue if you figure out which :P).