Enhancement request: support for user extensions on enums
https://github.com/rbtcollins/unleash-client-rust/tree/enum-dispatch contains my WIP in progress showing what I'd like to be able to do (in parallel with the old boxed approach, so that I can bench them side by side).
The basic problem I'm trying to solve is to be able to use enum-dispatch, but have some of the implementations contributed by clients of the crate. The domain of the crate is a client of an API, where the API is user extensible, so this is an intrinsic problem.
What limits this right now is that both ends of the enum_dispatch macro seem to want to know all the symbols for the code generation - and I don't fully understand why.
Right now
In my crate:
#[enum_dispatch]
pub trait Evaluator{
}
In a client crate:
use ...Evaluator
#[enum_dispatch(Evaluator)]
pub enum Strategies {
Default
Custom1
}
And the error:
Compiling unleash-api-client v0.2.0
error[E0433]: failed to resolve: use of undeclared type or module `Strategies`
--> src\strategy.rs:29:1
|
29 | #[enum_dispatch]
| ^^^^^^^^^^^^^^^^ use of undeclared type or module `Strategies`
|
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0412]: cannot find type `Strategies` in this scope
I presume that the reason for this is that the From
impl is being generated at the trait call site or something similar.
I wonder if it would be possible to generate all the Enum related code gen where the Enum is locate, allowing it to be entirely separated into a different crate?
AIUI there are three locations to consider:
a) the trait site b) the implementation sites c) the enum site
For my use to be met, I need the trait site to be in my crate, the implementation sites to be in my crate and in client crates, and the enum site to be in the client crate.
The trait site has no code gen needed, but today enum-dispatch does generate some code there apparently.
Each implementation needs to generate From
and TryInto
. These involve two types; the Enum and the implementation; for my use case they can both be generated into the client crate safely, as the Enum type will always be local.
The Enum needs to generate a Trait implementation, which should live where the Enum is.
tl;dr: what do you think of modifying enum-dispatch to make it possible to output all the codegen entirely where the enum is?