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
//! Iterator type reconciliation for conditional branching.
//!
//! When a function returns an iterator whose concrete type depends on a runtime condition, Rust
//! requires all branches to produce the same type. This module provides sum types that unify
//! multiple iterator types sharing the same [`Item`](Iterator::Item), along with extension traits
//! to wrap iterators into the appropriate variant.
//!
//! The pattern scales to any number of branches: [`Reconciled2`] handles two-way branching,
//! [`Reconciled3`] handles three-way branching, and additional arities can be added as needed.
//!
//! # Examples
//!
//! Returning different iterator types from conditional branches:
//!
//! ```rust,no_run
//! # use std::ops::Range;
//! # use std::iter::Rev;
//! # use zhc_utils::iter::{Reconciled2, ReconcilerOf2};
//! fn numbers(ascending: bool) -> Reconciled2<Range<i32>, Rev<Range<i32>>> {
//! if ascending {
//! (0..10).reconcile_1_of_2()
//! } else {
//! (0..10).rev().reconcile_2_of_2()
//! }
//! }
//!
//! // Both branches yield the same Item type, so callers see a unified iterator:
//! for n in numbers(true) {
//! println!("{n}");
//! }
//! ```
//!
//! Three-way branching with [`Reconciled3`]:
//!
//! ```rust,no_run
//! # use std::iter::{once, Empty};
//! # use zhc_utils::iter::{Reconciled3, ReconcilerOf3};
//! enum Mode { None, Single, Many }
//!
//! fn values(mode: Mode) -> Reconciled3<Empty<u8>, std::iter::Once<u8>, std::vec::IntoIter<u8>> {
//! match mode {
//! Mode::None => std::iter::empty().reconcile_1_of_3(),
//! Mode::Single => once(42).reconcile_2_of_3(),
//! Mode::Many => vec![1, 2, 3].into_iter().reconcile_3_of_3(),
//! }
//! }
//! ```
/// A sum type unifying two iterator types with the same item type.
///
/// This enum wraps one of two possible iterator types, delegating iteration to whichever variant
/// is active. Use the [`ReconcilerOf2`] extension trait to construct instances from any iterator.
///
/// The type parameters `I1` and `I2` correspond to the first and second branches respectively.
/// When calling [`reconcile_1_of_2`](ReconcilerOf2::reconcile_1_of_2), the iterator becomes the
/// `I1` slot; when calling [`reconcile_2_of_2`](ReconcilerOf2::reconcile_2_of_2), it becomes `I2`.
/// Extension trait for wrapping an iterator into a two-branch [`Reconciled2`].
///
/// This trait is blanket-implemented for all iterators. The type parameter `I1` represents the
/// *other* iterator type in the reconciled sum — the one not being wrapped by the current call.
/// Rust infers this from the function's return type annotation.
/// A sum type unifying three iterator types with the same item type.
///
/// This enum wraps one of three possible iterator types, delegating iteration to whichever variant
/// is active. Use the [`ReconcilerOf3`] extension trait to construct instances from any iterator.
///
/// The type parameters `I1`, `I2`, and `I3` correspond to the first, second, and third branches
/// respectively. The position in the type parameter list matches the numeric suffix of the
/// reconcile method used to construct it.
/// Extension trait for wrapping an iterator into a three-branch [`Reconciled3`].
///
/// This trait is blanket-implemented for all iterators. The type parameters `I1` and `I2`
/// represent the *other* iterator types in the reconciled sum — those not being wrapped by the
/// current call. Rust infers these from the function's return type annotation.