I have a series of structs S1
, S2
, S3
…, which all implement trait S
.
There’s an enum E
, indicates which S it is.
Enum E { S1, S2, S3 }
(For some reason I cannot merge S structs into the enum).
Now I have a Context
contains fields:
Structs Context<‘a> {
…
kind: E
obj: Box<dyn S>
…
}
E
And Sx
Is decided on runtime, which from tcp stream.
Now I have a series of functions to operate on Context
:
func operate1<‘a>(context: &mut Context<‘a>, s: &mut S1)
func operate2<‘a>(context: &mut Context<‘a>, s: &mut S1)
func operate3<‘a>(context: &mut Context<‘a>, s: &mut S2)
func operate4<‘a>(context: &mut Context<‘a>, s: &mut S2)
func operate5<‘a>(context: &mut Context<‘a>, s: &mut S2)
The function need both specific S
and other Context
fields.
Now I write a wrapper function to steal dyn S
from Context
, downcast to &mut Sx
, and then return it after operate function.
My question is:
Can I make it to
func operate6<‘a>(context: &mut ContextOrAnyOtherStruct::<‘a, S3>)
And smartly manage and call these operate functions by E
Or Sx
in Context
?
(Get a container, contains operate6
or its wrapper, and loop in it, only when kind
is E::S3
, call operate6
)
Should I have a look to tower
or axum
?