I have a trait for types which process data:
pub trait Stage<I, O>: Sized {}
Here, I
and O
represent the input and output data types respectively. I want to create an implementation of Stage
which combines two other Stage
s:
pub struct CompoundStage<S0: Stage<_, _>, S1: Stage<_, _>>(S0, S1);
CompoundStage
must itself be a Stage
:
impl<S0: Stage<_, _>, S1: Stage<_, _>> Stage<S0::I, S1::O> for CompoundStage<S0, S1> {}
How can I constrain the type arguments of CompoundStage
such that the “intermediate” type is consistent (i.e. S0::O == S1::I
)? I know I could do it by adding another type parameter like below, but is there a cleaner way?
pub struct CompoundStage<S0: Stage<_, T>, S1: Stage<T, _>, T>(S0, S1);
impl<S0: Stage<_, T>, S1: Stage<T, _>, T> Stage<S0::I, S1::O> for CompoundStage<S0, S1, T> {}