opendp/combinators/chain/shr/
mod.rs

1#[cfg(feature = "partials")]
2mod partials;
3
4use crate::{
5    core::{Domain, Function, Measure, Measurement, Metric, MetricSpace, Transformation},
6    error::Fallible,
7};
8use std::ops::Shr;
9
10use super::{make_chain_mt, make_chain_pm, make_chain_tt};
11
12// CHAINING TRANSFORMATION WITH TRANSFORMATION
13// There are seven impls:
14// 6 for the combinations of
15//   {Transformation, Fallible<Transformation>, PartialTransformation} and
16//   {Transformation, PartialTransformation}
17//
18// Partial impls are in the partials.rs module.
19// Partials are never wrapped in Fallible, so Fallible<PartialTransformation> are not included in the impls.
20// On the RHS, Fallible<Transformation> is not included, because it is trivial to ?-unwrap.
21// The seventh impl is for (MI, MO) >> PartialTransformation chaining.
22
23// Transformation >> Transformation
24impl<DI, DX, DO, MI, MX, MO> Shr<Transformation<DX, DO, MX, MO>> for Transformation<DI, DX, MI, MX>
25where
26    DI: 'static + Domain,
27    DX: 'static + Domain,
28    DO: 'static + Domain,
29    MI: 'static + Metric,
30    MX: 'static + Metric,
31    MO: 'static + Metric,
32    (DI, MI): MetricSpace,
33    (DX, MX): MetricSpace,
34    (DO, MO): MetricSpace,
35{
36    type Output = Fallible<Transformation<DI, DO, MI, MO>>;
37
38    fn shr(self, rhs: Transformation<DX, DO, MX, MO>) -> Self::Output {
39        make_chain_tt(&rhs, &self)
40    }
41}
42
43// Fallible<Transformation> >> Transformation
44impl<DI, DX, DO, MI, MX, MO> Shr<Transformation<DX, DO, MX, MO>>
45    for Fallible<Transformation<DI, DX, MI, MX>>
46where
47    DI: 'static + Domain,
48    DX: 'static + Domain,
49    DO: 'static + Domain,
50    MI: 'static + Metric,
51    MX: 'static + Metric,
52    MO: 'static + Metric,
53    (DI, MI): MetricSpace,
54    (DX, MX): MetricSpace,
55    (DO, MO): MetricSpace,
56{
57    type Output = Fallible<Transformation<DI, DO, MI, MO>>;
58
59    fn shr(self, rhs: Transformation<DX, DO, MX, MO>) -> Self::Output {
60        make_chain_tt(&rhs, &self?)
61    }
62}
63
64// CHAINING TRANSFORMATION WITH MEASUREMENT
65// There are seven impls:
66// 6 for the combinations of
67//   {Transformation, Fallible<Transformation>, PartialTransformation} and
68//   {Measurement, PartialMeasurement}
69//
70// Partial impls are in the partials.rs module.
71// Partials are never wrapped in Fallible, so Fallible<PartialTransformation> are not included in the impls.
72// On the RHS, Fallible<Measurement> is not included, because it is trivial to ?-unwrap.
73// The seventh impl is for (MI, MO) >> PartialMeasurement chaining.
74
75// Transformation >> Measurement
76impl<DI, DX, TO, MI, MX, MO> Shr<Measurement<DX, TO, MX, MO>> for Transformation<DI, DX, MI, MX>
77where
78    DI: 'static + Domain,
79    DX: 'static + Domain,
80    TO: 'static,
81    MI: 'static + Metric,
82    MX: 'static + Metric,
83    MO: 'static + Measure,
84    (DI, MI): MetricSpace,
85    (DX, MX): MetricSpace,
86{
87    type Output = Fallible<Measurement<DI, TO, MI, MO>>;
88
89    fn shr(self, rhs: Measurement<DX, TO, MX, MO>) -> Self::Output {
90        make_chain_mt(&rhs, &self)
91    }
92}
93
94// Fallible<Transformation> >> Measurement
95impl<DI, DX, TO, MI, MX, MO> Shr<Measurement<DX, TO, MX, MO>>
96    for Fallible<Transformation<DI, DX, MI, MX>>
97where
98    DI: 'static + Domain,
99    DX: 'static + Domain,
100    TO: 'static,
101    MI: 'static + Metric,
102    MX: 'static + Metric,
103    MO: 'static + Measure,
104    (DI, MI): MetricSpace,
105    (DX, MX): MetricSpace,
106{
107    type Output = Fallible<Measurement<DI, TO, MI, MO>>;
108
109    fn shr(self, rhs: Measurement<DX, TO, MX, MO>) -> Self::Output {
110        make_chain_mt(&rhs, &self?)
111    }
112}
113// CHAINING POSTPROCESS WITH MEASUREMENT
114// There are seven impls:
115// 6 for the combinations of
116//   {Measurement, Fallible<Measurement>, PartialMeasurement} and
117//   {Function, Transformation}
118//
119// Partial impls are in the partials.rs module.
120// Partials are never wrapped in Fallible, so Fallible<PartialTransformation> are not included in the impls.
121// On the RHS, Fallible<Measurement> is not included, because it is trivial to ?-unwrap.
122// The seventh impl is for Function >> Function chaining.
123
124// Measurement >> Function
125impl<DI, TX, TO, MI, MO> Shr<Function<TX, TO>> for Measurement<DI, TX, MI, MO>
126where
127    DI: 'static + Domain,
128    TX: 'static,
129    TO: 'static,
130    MI: 'static + Metric,
131    MO: 'static + Measure,
132    (DI, MI): MetricSpace,
133{
134    type Output = Fallible<Measurement<DI, TO, MI, MO>>;
135
136    fn shr(self, rhs: Function<TX, TO>) -> Self::Output {
137        make_chain_pm(&rhs, &self)
138    }
139}
140
141// Fallible<Measurement> >> Function
142impl<DI, TX, TO, MI, MO> Shr<Function<TX, TO>> for Fallible<Measurement<DI, TX, MI, MO>>
143where
144    DI: 'static + Domain,
145    TX: 'static,
146    TO: 'static,
147    MI: 'static + Metric,
148    MO: 'static + Measure,
149    (DI, MI): MetricSpace,
150{
151    type Output = Fallible<Measurement<DI, TO, MI, MO>>;
152
153    fn shr(self, rhs: Function<TX, TO>) -> Self::Output {
154        make_chain_pm(&rhs, &self?)
155    }
156}
157
158// Measurement >> Transformation
159impl<DI, DX, DO, MI, MO, MTI, MTO> Shr<Transformation<DX, DO, MTI, MTO>>
160    for Measurement<DI, DX::Carrier, MI, MO>
161where
162    DI: 'static + Domain,
163    DX: 'static + Domain,
164    DO: 'static + Domain,
165    MI: 'static + Metric,
166    MO: 'static + Measure,
167    MTI: 'static + Metric,
168    MTO: 'static + Metric,
169    (DI, MI): MetricSpace,
170    (DX, MTI): MetricSpace,
171    (DO, MTO): MetricSpace,
172{
173    type Output = Fallible<Measurement<DI, DO::Carrier, MI, MO>>;
174
175    fn shr(self, rhs: Transformation<DX, DO, MTI, MTO>) -> Self::Output {
176        make_chain_pm(&rhs.function, &self)
177    }
178}
179
180// Fallible<Measurement> >> Transformation
181impl<DI, DX, DO, MI, MO, MTI, MTO> Shr<Transformation<DX, DO, MTI, MTO>>
182    for Fallible<Measurement<DI, DX::Carrier, MI, MO>>
183where
184    DI: 'static + Domain,
185    DX: 'static + Domain,
186    DO: 'static + Domain,
187    MI: 'static + Metric,
188    MO: 'static + Measure,
189    MTI: 'static + Metric,
190    MTO: 'static + Metric,
191    (DI, MI): MetricSpace,
192    (DX, MTI): MetricSpace,
193    (DO, MTO): MetricSpace,
194{
195    type Output = Fallible<Measurement<DI, DO::Carrier, MI, MO>>;
196
197    fn shr(self, rhs: Transformation<DX, DO, MTI, MTO>) -> Self::Output {
198        make_chain_pm(&rhs.function, &self?)
199    }
200}
201
202// Function >> Function
203impl<TI, TX, TO> Shr<Function<TX, TO>> for Function<TI, TX>
204where
205    TI: 'static,
206    TX: 'static,
207    TO: 'static,
208{
209    type Output = Function<TI, TO>;
210
211    fn shr(self, rhs: Function<TX, TO>) -> Self::Output {
212        Function::make_chain(&rhs, &self)
213    }
214}