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
//! Map resolver.
use super::*;
impl<P: Copy, R: Copy, const D: Dep> I<ValidH<P, R>, D> {
/// Maps the egress resolver into the ingress resolver.
///
/// - Payload: Preserved.
/// - Resolver: Mapped by `f`.
///
/// | Interface | Ingress | Egress |
/// | :-------: | ------------ | ------------ |
/// | **Fwd** | `HOption<P>` | `HOption<P>` |
/// | **Bwd** | `R` | `ER` |
pub fn map_resolver<ER: Copy>(self, f: impl Fn(ER) -> R) -> I<ValidH<P, ER>, D> {
unsafe { self.fsm::<(), D, ValidH<P, ER>>((), |ip, er, s| (ip, f(er), s)) }
}
}
impl<P: Copy, R: Copy> I<ValidH<P, R>, { Dep::Helpful }> {
/// A variation of [`map_resolver`] that allows `f` to consider the ingress payload in addition to the egress
/// resolver while calculating the ingress resolver.
///
/// This is possible because the ingress interface is [`Dep::Helpful`].
///
/// - Payload: Preserved.
/// - Resolver: Mapped by `f`.
///
/// | Interface | Ingress | Egress |
/// | :-------: | ------------ | ------------ |
/// | **Fwd** | `HOption<P>` | `HOption<P>` |
/// | **Bwd** | `R` | `ER` |
pub fn map_resolver_with_p<ER: Copy>(self, f: impl Fn(HOption<P>, ER) -> R) -> I<ValidH<P, ER>, { Dep::Helpful }> {
unsafe { self.fsm::<(), { Dep::Helpful }, ValidH<P, ER>>((), |ip, er, s| (ip, f(ip, er), s)) }
}
}
impl<P: Copy, R: Copy, const D: Dep> I<VrH<P, R>, D> {
/// Maps the egress resolver into the ingress resolver.
///
/// Note that it disallows changing the ready signal, since if `D` is [`Dep::Demanding`], changing the ready signal
/// may break the [`Dep::Demanding`] condition.
///
/// - Payload: Preserved.
/// - Resolver: The egress resolver `Ready<ER>` is mapped to the inner value `R` of the ingress resolver by `f`.
///
/// | Interface | Ingress | Egress |
/// | :-------: | ------------ | ------------ |
/// | **Fwd** | `HOption<P>` | `HOption<P>` |
/// | **Bwd** | `Ready<R>` | `Ready<ER>` |
pub fn map_resolver<ER: Copy>(self, f: impl Fn(Ready<ER>) -> R) -> I<VrH<P, ER>, D> {
unsafe { self.fsm::<(), D, VrH<P, ER>>((), |ip, er, s| (ip, Ready::new(er.ready, f(er)), s)) }
}
/// A variation of [`map_resolver`] that does not use the egress ready signal.
///
/// - Payload: Preserved.
/// - Resolver: The inner value `ER` of the egress resolver is mapped into the inner value `R` of the ingress
/// resolver by `f`.
///
/// | Interface | Ingress | Egress |
/// | :-------: | ------------ | ------------ |
/// | **Fwd** | `HOption<P>` | `HOption<P>` |
/// | **Bwd** | `Ready<R>` | `Ready<ER>` |
pub fn map_resolver_inner<ER: Copy>(self, f: impl Fn(ER) -> R) -> I<VrH<P, ER>, D> {
self.map_resolver(|er| f(er.inner))
}
}
impl<P: Copy, R: Copy> I<VrH<P, R>, { Dep::Helpful }> {
/// A variation of [`map_resolver`] that allows `f` to consider the ingress payload in addition to the egress
/// resolver while calculating the ingress resolver.
///
/// This is possible because the ingress interface is [`Dep::Helpful`].
///
/// Note that it allows changing the ready signal unlike the original [`map_resolver`], since the egress interface
/// is [`Dep::Helpful`].
///
/// - Payload: Preserved.
/// - Resolver: Mapped by `f`.
///
/// | Interface | Ingress | Egress |
/// | :-------: | ------------ | ------------ |
/// | **Fwd** | `HOption<P>` | `HOption<P>` |
/// | **Bwd** | `R` | `ER` |
pub fn map_resolver_with_p<ER: Copy>(
self,
f: impl Fn(HOption<P>, Ready<ER>) -> Ready<R>,
) -> I<VrH<P, ER>, { Dep::Helpful }> {
unsafe { self.fsm::<(), { Dep::Helpful }, VrH<P, ER>>((), |ip, er, s| (ip, f(ip, er), s)) }
}
/// A variation of [`I::map_resolver_inner`] that allows `f` to consider the ingress payload in addition to the
/// egress resolver while calculating the ingress resolver.
///
/// This is possible because the ingress interface is [`Dep::Helpful`].
///
/// - Payload: Preserved.
/// - Resolver: The inner value `ER` of the egress resolver is mapped into the inner value `R` of the ingress
/// resolver by `f`.
///
/// | Interface | Ingress | Egress |
/// | :-------: | ------------ | ------------ |
/// | **Fwd** | `HOption<P>` | `HOption<P>` |
/// | **Bwd** | `Ready<R>` | `Ready<ER>` |
pub fn map_resolver_inner_with_p<ER: Copy>(
self,
f: impl Fn(HOption<P>, ER) -> R,
) -> I<VrH<P, ER>, { Dep::Helpful }> {
self.map_resolver_with_p(|ip, er| Ready::new(er.ready, f(ip, er.inner)))
}
}
impl<H: Hazard, const D: Dep> I<H, D> {
/// Maps the egress resolver into the ingress resolver.
///
/// - Payload: Droppped if `H::ready(ip, ir)` or `EH::ready(ep, er)` is false.
/// ([why?](super#notes-on-dropping-combinators))
/// - Resolver: Mapped by `f`.
///
/// | Interface | Ingress | Egress |
/// | :-------: | --------------- | --------------- |
/// | **Fwd** | `HOption<H::P>` | `HOption<H::P>` |
/// | **Bwd** | `H::R` | `EH::R` |
pub fn map_resolver_drop<EH: Hazard<P = H::P>>(self, f: impl Fn(EH::R) -> H::R) -> I<EH, { Dep::Demanding }> {
unsafe {
self.fsm::<(), { Dep::Demanding }, EH>((), |ip, er, s| {
let ir = f(er);
let ep = if ip.is_some_and(|p| H::ready(p, ir) && EH::ready(p, er)) { ip } else { None };
(ep, ir, s)
})
}
}
}
impl<H: Hazard> I<H, { Dep::Helpful }> {
/// A variation of [`I::map_resolver_drop`] that allows `f` to consider the ingress payload in addition to the
/// egress resolver while calculating the ingress resolver.
///
/// This is possible because the ingress interface is [`Dep::Helpful`].
///
/// - Payload: The same behavior as [`I::map_resolver_drop`]
/// - Resolver: The same behavior as [`I::map_resolver_drop`]
///
/// | Interface | Ingress | Egress |
/// | :-------: | --------------- | --------------- |
/// | **Fwd** | `HOption<H::P>` | `HOption<H::P>` |
/// | **Bwd** | `H::R` | `EH::R` |
pub fn map_resolver_drop_with_p<EH: Hazard<P = H::P>>(
self,
f: impl Fn(HOption<H::P>, EH::R) -> H::R,
) -> I<EH, { Dep::Demanding }> {
unsafe {
self.fsm::<(), { Dep::Demanding }, EH>((), |ip, er, s| {
let ir = f(ip, er);
let ep = if ip.is_some_and(|p| H::ready(p, ir) && EH::ready(p, er)) { ip } else { None };
(ep, ir, s)
})
}
}
}
impl<H: Hazard> I<AndH<H>, { Dep::Helpful }> {
/// Maps the egress resolver into the ingress resolver with an additional ready signal for blocking.
///
/// - Payload: Preserved. Technically, the payload is dropped if `H::ready(ip, ir)` or `EH::ready(ep, er)` is false.
/// But since the added ready signal informs the ingress interface that a transfer did not happen (i.e. blocks
/// the transfer) in such a case, no payload will be lost.
/// - Resolver: An additional ready signal is attached to the ingress resolver, which will be turned off if the
/// egress ready condition `EH::ready(ep, er)` is false. The egress resolver `EH::R` is mapped to the inner
/// ingress resolver `H::R` by `f`.
///
/// | Interface | Ingress | Egress |
/// | :-------: | --------------- | --------------- |
/// | **Fwd** | `HOption<H::P>` | `HOption<H::P>` |
/// | **Bwd** | `Ready<H::R>` | `EH::R` |
pub fn map_resolver_block<EH: Hazard<P = H::P>>(self, f: impl Fn(EH::R) -> H::R) -> I<EH, { Dep::Demanding }> {
self.map_resolver_block_with_p(|_, er| f(er))
}
/// A variation of [`I::map_resolver_block`] that allows `f` to consider the ingress payload in addition to the
/// egress resolver while calculating the ingress resolver.
///
/// This is possible because the ingress interface is [`Dep::Helpful`].
///
/// - Payload: The same behavior as [`I::map_resolver_block`].
/// - Resolver: The same behavior as [`I::map_resolver_block`].
///
/// | Interface | Ingress | Egress |
/// | :-------: | --------------- | --------------- |
/// | **Fwd** | `HOption<H::P>` | `HOption<H::P>` |
/// | **Bwd** | `Ready<H::R>` | `EH::R` |
pub fn map_resolver_block_with_p<EH: Hazard<P = H::P>>(
self,
f: impl Fn(HOption<H::P>, EH::R) -> H::R,
) -> I<EH, { Dep::Demanding }> {
unsafe {
self.fsm::<(), { Dep::Demanding }, EH>((), |ip, er, s| {
let ir_inner = f(ip, er);
let xfer = ip.is_some_and(|p| H::ready(p, ir_inner) && EH::ready(p, er));
let ir = Ready::new(xfer, ir_inner);
let ep = if xfer { ip } else { None };
(ep, ir, s)
})
}
}
}