I have a trait ProcessNode
which essentially is just a stateful function that takes in an input A and outputs B:
pub trait ProcessNode<A, B> {
fn process(&mut self, input: A) -> B;
}
Then I have a struct Branch
which is also a ProcessNode
. All Branch
does is take two ProcessNodes, feeds the input into one, feeds the output of that one to the other, and then returns the output of both. Since it would be inefficient to clone the output of the first node, I want to pass it as reference to the second node to generate the second output. This is what I currently have:
pub struct Branch<'a, A, B, C, D: ProcessNode<A, B>, E: ProcessNode<&'a B, C>> {
_marker: PhantomData<(A, &'a B, C)>,
node1: D,
node2: E
}
impl <'a, A, B, C, D: ProcessNode<A, B>, E: ProcessNode<&'a B, C>>ProcessNode<A, (B,C)> for Branch<'a, A, B, C, D, E> {
fn process(&mut self, input: A) -> (B, C) {
let out1 = self.node1.process(input);
let out2 = self.node2.process(&out1);
(out1, out2)
}
}
This throws an error because the refrence I pass to the second function doesn't live for 'a. I could make another generic that would be the input of the second function, but then the output of the first and the input of the second will not be linked. Is there a way to solve this?