feature request: allow naming an existing associated type rather than generating one automatically
I have some code that is using the type state pattern + an enum dispatch pattern, and it can be hand written, but it would be great if enum_delegate would work for it. I might even supply a patch, if this is of interest.
The handcrafted code looks something like like
#[async_trait]
trait S1 {
type S2: S2;
async fn to_s2(self) -> Self::S2;
}
#[async_trait]
trait S2 {
type S1: S1;
async fn to_s1(self) -> Self::S1;
}
enum S1s {
Imp1(Imp1S1),
Imp2(Imp2S1),
}
#[async_trait]
impl S1 for S1s {
type S2 = S2s;
async fn to_s2(self) -> Self::S2 {
match self {
S1s::Imp1(s) => Ok(S2s::Imp1(s.to_s2().await)),
S1s::Imp2(s) => Ok(S2s::Imp2(s.to_s2().await)),
}
}
}
enum S2s {
Imp1(Imp1S2),
Imp1(Imp2S2),
}
#[async_trait]
impl S2 for S2s {
type S1 = S1s;
async fn to_s1(self) -> Self::S1 {
match self {
S2s::Imp1(s) => Ok(S1s::Imp1(s.stop().await)),
S2s::Imp2(s) => Ok(S1s::Imp2(s.stop().await)),
}
}
}
struct Imp1S1;
#[async_trait]
impl S1 for Imp1 {
type S2 = Imp1S2;
async fn to_s2(self) -> Self::S2 {
unimplemented!();
}
}
struct Imp1S2;
#[async_trait]
impl S2 for Imp1S2 {
type S1 = Imp1S1;
async fn to_s1(self) -> Self::S1 {
unimplemented!();
}
}
... elided Imp2S1 and Imp2S2
I think all this requires is being able to name the associated types - which must be an enum that contains at least every delegated concrete type present in the delegating enum