1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
    Appellation: prop <mod>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::error::PredictError;

/// [Backward] describes an object capable of backward propagation.
///
///  
pub trait Backward {
    type Output;

    fn backward(&self) -> Self::Output;
}

/// [Forward] describes an object capable of forward propagation.
pub trait Forward<T> {
    type Output;

    fn forward(&self, args: &T) -> Result<Self::Output, PredictError>;
}

pub trait ForwardIter<T> {
    type Item: Forward<T, Output = T>;

    fn forward_iter(self, args: &T) -> Result<<Self::Item as Forward<T>>::Output, PredictError>;
}

// Trait implementations
mod impls {
    use super::*;

    impl<I, M, T> ForwardIter<T> for I
    where
        I: Iterator<Item = M>,
        M: Forward<T, Output = T>,
        T: Clone,
    {
        type Item = M;

        fn forward_iter(self, args: &T) -> Result<M::Output, PredictError> {
            let mut result = args.clone();
            for i in self {
                result = i.forward(&result)?;
            }
            Ok(result)
        }
    }

    impl<S, T> Forward<T> for Option<S>
    where
        S: Forward<T, Output = T>,
        T: Clone,
    {
        type Output = T;

        fn forward(&self, args: &T) -> Result<Self::Output, PredictError> {
            match self {
                Some(s) => s.forward(args),
                None => Ok(args.clone()),
            }
        }
    }

    impl<S, T> Forward<T> for S
    where
        S: AsRef<dyn Forward<T, Output = T>>,
        T: Clone,
    {
        type Output = T;

        fn forward(&self, args: &T) -> Result<Self::Output, PredictError> {
            self.as_ref().forward(args)
        }
    }
}