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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//! FSM mapping combinators.

use super::*;

impl<P: Copy, R: Copy, const D: Dep> I<ValidH<P, R>, D> {
    /// A [`map`] with an internal state.
    ///
    /// `f` additionally takes the current state and returns the next state. The state is updated if the ingress payload
    /// is valid and so an ingress transfer happens.
    ///
    /// - Payload: Mapped by `f`.
    /// - Resolver: Preserved.
    ///
    /// | Interface | Ingress      | Egress        |
    /// | :-------: | ------------ | ------------- |
    /// |  **Fwd**  | `HOption<P>` | `HOption<EP>` |
    /// |  **Bwd**  | `R`          | `R`           |
    pub fn fsm_map<EP: Copy, S: Copy>(self, init_state: S, f: impl Fn(P, S) -> (EP, S)) -> I<ValidH<EP, R>, D> {
        self.map_resolver::<(R, S)>(|(r, _)| r).transparent_fsm_map(init_state, f)
    }

    /// A [`filter_map`] with an internal state.
    ///
    /// `f` additionally takes the current state and returns the next state. The state is updated if the ingress payload
    /// is valid and so an ingress transfer happens.
    ///
    /// - Payload: Filter-mapped by `f`.
    /// - Resolver: Preserved.
    ///
    /// | Interface | Ingress      | Egress        |
    /// | :-------: | ------------ | ------------- |
    /// |  **Fwd**  | `HOption<P>` | `HOption<EP>` |
    /// |  **Bwd**  | `R`          | `R`           |
    pub fn fsm_filter_map<EP: Copy, S: Copy>(
        self,
        init: S,
        f: impl Fn(P, S) -> (HOption<EP>, S),
    ) -> I<ValidH<EP, R>, D> {
        self.map_resolver::<(R, S)>(|(r, _)| r).transparent_fsm_filter_map(init, f)
    }
}

impl<P: Copy, R: Copy, S: Copy, const D: Dep> I<ValidH<P, (R, S)>, D> {
    /// A variation of [`fsm_map`] that attaches an additional internal state signal to the ingress resolver.
    ///
    /// - Payload: Mapped by `f`.
    /// - Resolver: Preserved. The internal state `S` is additionally outputted.
    ///
    /// | Interface | Ingress      | Egress        |
    /// | :-------: | ------------ | ------------- |
    /// |  **Fwd**  | `HOption<P>` | `HOption<EP>` |
    /// |  **Bwd**  | `(R, S)`     | `R`           |
    pub fn transparent_fsm_map<EP: Copy>(self, init_state: S, f: impl Fn(P, S) -> (EP, S)) -> I<ValidH<EP, R>, D> {
        self.transparent_fsm_filter_map(init_state, |p, s| {
            let (ep, s_next) = f(p, s);
            (Some(ep), s_next)
        })
    }

    /// A variation of [`transparent_fsm_map`](fsm_map) that allows `f` to additionally consider the egress resolver.
    ///
    /// - Payload: Mapped by `f`.
    /// - Resolver: Preserved. The internal state `S` is additionally outputted.
    ///
    /// | Interface | Ingress      | Egress        |
    /// | :-------: | ------------ | ------------- |
    /// |  **Fwd**  | `HOption<P>` | `HOption<EP>` |
    /// |  **Bwd**  | `(R, S)`     | `R`           |
    pub fn transparent_fsm_map_with_r<EP: Copy>(
        self,
        init_state: S,
        f: impl Fn(P, R, S) -> (EP, S),
    ) -> I<ValidH<EP, R>, { Dep::Demanding }> {
        self.transparent_fsm_filter_map_with_r(init_state, |p, r, s| {
            let (ep, s_next) = f(p, r, s);
            (Some(ep), s_next)
        })
    }

    /// A variation of [`fsm_filter_map`](fsm_map) that attaches an additional internal state signal to the ingress
    /// resolver.
    ///
    /// - Payload: Filter-mapped by `f`.
    /// - Resolver: Preserved. The internal state `S` is additionally outputted.
    ///
    /// | Interface | Ingress      | Egress        |
    /// | :-------: | ------------ | ------------- |
    /// |  **Fwd**  | `HOption<P>` | `HOption<EP>` |
    /// |  **Bwd**  | `(R, S)`     | `R`           |
    pub fn transparent_fsm_filter_map<EP: Copy>(
        self,
        init: S,
        f: impl Fn(P, S) -> (HOption<EP>, S),
    ) -> I<ValidH<EP, R>, D> {
        unsafe {
            self.fsm(init, |ip, er, s| {
                let (ep, s_next) = match ip {
                    Some(p) => f(p, s),
                    None => (None, s),
                };
                (ep, (er, s), s_next)
            })
        }
    }

    /// A variation of [`transparent_fsm_filter_map`](fsm_map) that allows `f` to additionally consider the egress resolver.
    ///
    /// - Payload: Filter-mapped by `f`.
    /// - Resolver: Preserved. The internal state `S` is additionally outputted.
    ///
    /// | Interface | Ingress      | Egress        |
    /// | :-------: | ------------ | ------------- |
    /// |  **Fwd**  | `HOption<P>` | `HOption<EP>` |
    /// |  **Bwd**  | `(R, S)`     | `R`           |
    pub fn transparent_fsm_filter_map_with_r<EP: Copy>(
        self,
        init: S,
        f: impl Fn(P, R, S) -> (HOption<EP>, S),
    ) -> I<ValidH<EP, R>, { Dep::Demanding }> {
        unsafe {
            self.fsm(init, |ip, er, s| {
                let (ep, s_next) = match ip {
                    Some(p) => f(p, er, s),
                    None => (None, s),
                };
                (ep, (er, s), s_next)
            })
        }
    }
}

impl<P: Copy, R: Copy, const D: Dep> I<VrH<P, R>, D> {
    /// A [`map`] with an internal state.
    ///
    /// `f` additionally takes the current state and returns the next state. The state is updated if an ingress transfer
    /// happens.
    ///
    /// - Payload: Mapped by `f`.
    /// - Resolver: Preserved.
    ///
    /// | Interface | Ingress      | Egress        |
    /// | :-------: | ------------ | ------------- |
    /// |  **Fwd**  | `HOption<P>` | `HOption<EP>` |
    /// |  **Bwd**  | `Ready<R>`   | `Ready<R>`    |
    pub fn fsm_map<EP: Copy, S: Copy>(self, init_state: S, f: impl Fn(P, S) -> (EP, S)) -> I<VrH<EP, R>, D> {
        self.map_resolver_inner::<(R, S)>(|(r, _)| r).transparent_fsm_map(init_state, f)
    }

    /// A [`filter_map`] with an internal state.
    ///
    /// `f` additionally takes the current state and returns the next state. The state is updated if an ingress transfer
    /// happens.
    ///
    /// - Payload: Filter-mapped by `f`.
    /// - Resolver: Preserved.
    ///
    /// | Interface | Ingress      | Egress        |
    /// | :-------: | ------------ | ------------- |
    /// |  **Fwd**  | `HOption<P>` | `HOption<EP>` |
    /// |  **Bwd**  | `Ready<R>`   | `Ready<R>`    |
    pub fn fsm_filter_map<EP: Copy, S: Copy>(self, init: S, f: impl Fn(P, S) -> (HOption<EP>, S)) -> I<VrH<EP, R>, D> {
        self.map_resolver_inner::<(R, S)>(|(r, _)| r).transparent_fsm_filter_map(init, f)
    }
}

impl<P: Copy, R: Copy, S: Copy, const D: Dep> I<VrH<P, (R, S)>, D> {
    /// A variation of [`fsm_map`] that attaches an additional internal state signal to the ingress resolver.
    ///
    /// - Payload: Mapped by `f`.
    /// - Resolver: Preserved. The internal state `S` is additionally outputted.
    ///
    /// | Interface | Ingress         | Egress        |
    /// | :-------: | --------------- | ------------- |
    /// |  **Fwd**  | `HOption<P>`    | `HOption<EP>` |
    /// |  **Bwd**  | `Ready<(R, S)>` | `Ready<R>`    |
    pub fn transparent_fsm_map<EP: Copy>(self, init_state: S, f: impl Fn(P, S) -> (EP, S)) -> I<VrH<EP, R>, D> {
        self.transparent_fsm_filter_map(init_state, |p, s| {
            let (ep, s_next) = f(p, s);
            (Some(ep), s_next)
        })
    }

    /// A variation of [`transparent_fsm_map`](fsm_map) that allows `f` to additionally consider the egress resolver.
    ///
    /// - Payload: Mapped by `f`. The payload is dropped if `er.ready` is false, even if `f` returns `Some`.
    ///     ([why?](super#notes-on-dropping-combinators))
    /// - Resolver: Preserved. The internal state `S` is additionally outputted.
    ///
    /// | Interface | Ingress         | Egress        |
    /// | :-------: | --------------- | ------------- |
    /// |  **Fwd**  | `HOption<P>`    | `HOption<EP>` |
    /// |  **Bwd**  | `Ready<(R, S)>` | `Ready<R>`    |
    pub fn transparent_fsm_map_drop_with_r<EP: Copy>(
        self,
        init: S,
        f: impl Fn(P, Ready<R>, S) -> (EP, S),
    ) -> I<VrH<EP, R>, { Dep::Demanding }> {
        self.transparent_fsm_filter_map_drop_with_r(init, |p, r, s| {
            let (ep, s_next) = f(p, r, s);
            (Some(ep), s_next)
        })
    }

    /// A variation of [`fsm_filter_map`](fsm_map) that attaches an additional internal state signal to the ingress
    /// resolver.
    ///
    /// - Payload: Filter-mapped by `f`.
    /// - Resolver: Preserved. The internal state `S` is additionally outputted.
    ///
    /// | Interface | Ingress         | Egress        |
    /// | :-------: | --------------- | ------------- |
    /// |  **Fwd**  | `HOption<P>`    | `HOption<EP>` |
    /// |  **Bwd**  | `Ready<(R, S)>` | `Ready<R>`    |
    pub fn transparent_fsm_filter_map<EP: Copy>(
        self,
        init: S,
        f: impl Fn(P, S) -> (HOption<EP>, S),
    ) -> I<VrH<EP, R>, D> {
        unsafe {
            self.fsm::<S, D, VrH<EP, R>>(init, |ip, er, s| {
                let (ep, s_next) = match ip {
                    Some(ip) => f(ip, s),
                    None => (None, s),
                };
                let ir = er.map(|r| (r, s));
                (ep, ir, if er.ready { s_next } else { s })
            })
        }
    }

    /// A variation of [`transparent_fsm_filter_map`](fsm_map) that allows `f` to additionally consider the egress resolver.
    ///
    /// - Payload: Filter-mapped by `f`. The payload is dropped if `er.ready` is false, even if `f` returns `Some`.
    ///     ([why?](super#notes-on-dropping-combinators))
    /// - Resolver: Preserved. The internal state `S` is additionally outputted.
    ///
    /// | Interface | Ingress         | Egress        |
    /// | :-------: | --------------- | ------------- |
    /// |  **Fwd**  | `HOption<P>`    | `HOption<EP>` |
    /// |  **Bwd**  | `Ready<(R, S)>` | `Ready<R>`    |
    pub fn transparent_fsm_filter_map_drop_with_r<EP: Copy>(
        self,
        init: S,
        f: impl Fn(P, Ready<R>, S) -> (HOption<EP>, S),
    ) -> I<VrH<EP, R>, { Dep::Demanding }> {
        unsafe {
            self.fsm::<S, { Dep::Demanding }, VrH<EP, R>>(init, |ip, er, s| {
                let (ep, s_next) = match ip {
                    Some(ip) if er.ready => f(ip, er, s),
                    _ => (None, s),
                };
                let ir = er.map(|r| (r, s));
                (ep, ir, s_next)
            })
        }
    }
}