signal_functions/
accum.rs

1use crate::*;
2
3use std::marker::PhantomData;
4
5pub struct Accum<Acc, Input, C, F> {
6    acc: Acc,
7    f: F,
8    phantom: PhantomData<Input>,
9    phantom_c: PhantomData<C>,
10}
11
12impl<Acc, Input, C, F: FnMut(&mut Acc, Input)> Accum<Acc, Input, C, F> {
13    pub fn new(acc: Acc, f: F) -> Self {
14        Accum {
15            acc,
16            f,
17            phantom: PhantomData,
18            phantom_c: PhantomData,
19        }
20    }
21}
22
23impl<Acc: Clone, Input, C, F: FnMut(&mut Acc, Input)> StreamFunction for Accum<Acc, Input, C, F> {
24    type Input = Input;
25    type Output = Acc;
26    type Clock = C;
27    type Error = (); // Actually I'd like to use !
28
29    fn step(&mut self, input: Self::Input, _: Self::Clock) -> Result<Self::Output, Self::Error> {
30        let acc = self.acc.clone();
31        (self.f)(&mut self.acc, input);
32        Ok(acc)
33    }
34}