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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//! Owned state of ONE [`super::h2::H2Shell::write_streams`] pass.
//!
//! Every value here used to be a `let` inside that function. They are grouped
//! into one struct because the write path is being turned into a drive loop —
//! a `poll` that names the next transmit, a shell that performs it, a `handle`
//! that reports it back — and a value the pre-image kept in a stack slot
//! across the flush loop has to survive that round trip. Extracting the state
//! first, with the control flow left exactly as it was, keeps the two halves
//! separately reviewable: this half moves no condition, no ordering and no
//! saturating-add boundary.
//!
//! **This module owns**: the byte counters, flags and scratch vectors of one
//! write pass, and their initial values.
//!
//! **This module also owns**, since the inversion, the pass's PHASE
//! ([`H2WritePhase`]) and the three values the drive loop has to carry across
//! the caller boundary without being able to build them at pass start: the
//! [`super::converter::H2ConverterPass`], the scheduler's loaned `order`
//! buffer and its [`super::h2_scheduler::ReadyIncrementalCensus`]. They are
//! LATE-INITIALISED — see [`H2WritePass::adopt_scheduler_pass`] — and the two
//! reasons they cannot be pass-start fields are different reasons, not one:
//!
//! - `order` and `census` are built by `H2Scheduler::begin_pass` and given
//! back to `H2Scheduler::end_pass`. `begin_pass` enumerates
//! `stream_table.streams()`, and the `expect_write` resume path that runs
//! BEFORE it can retire a stream out of that same map through
//! `ConnectionH2::remove_dead_stream` — so building them at pass start would
//! change which streams the pass visits.
//! - The `H2ConverterPass` enumerates no streams, so the reason above does NOT
//! apply to it; its reason is buffer stranding. Its constructor takes the
//! three reusable scratch buffers out of `HpackState` (`take_converter_buf`
//! / `take_lowercase_buf` / `take_cookie_buf`, each a `mem::take`), and only
//! `into_buffers` hands them back through the matching `put_*` calls. The
//! resume path's stall ends the pass before both, so a converter built at
//! pass start would be dropped on every stalled resume, leaving `HpackState`
//! holding three empty `Vec`s to re-grow — the reuse the pass exists to
//! provide, discarded on exactly the backpressure path that repeats most.
//!
//! **This module does NOT own** the pass's control flow — that is
//! `ConnectionH2::poll_write_target` — nor the `Vec<IoSlice<'static>>`. The
//! vector IS buildable at pass start and is still excluded, by design rather
//! than by necessity: `h2_transmit::gather` returns descriptors carrying a
//! `'static` lifetime they do not have, pointing into `kawa.storage`, and
//! `h2_transmit::confirm` must discharge them before the consume that may
//! relocate that storage. Leaving the vector with the shell keeps that
//! `unsafe` window exactly as wide as the three adjacent statements that open
//! and close it, and stops the poll/handle boundary from spanning it. The
//! shell keeps it as a private field for the connection's lifetime, so its
//! capacity is reused from one pass to the next and a warm pass allocates
//! nothing for it.
//!
//! The pass is a local threaded by `&mut`, never a field on `ConnectionH2`:
//! every exit of `write_streams` ends it, and every phase transition falls
//! through to [`H2WritePhase::End`] or to a `Done`, so it cannot outlive one
//! call. Resumption ACROSS calls is already expressed by
//! `H2StreamTable::expect_write`.
use ;
/// What every late-initialised accessor below panics with.
///
/// ONE message and ONE `expect` per accessor, so a violation names the
/// invariant rather than a line number: the three scheduler-pass values are
/// absent in [`H2WritePhase::Start`], [`H2WritePhase::Resume`] and
/// [`H2WritePhase::Ended`], present from the transition into
/// [`H2WritePhase::Prepare`], and taken back at the first statement of
/// [`H2WritePhase::End`], which then moves the pass to `Ended`.
///
/// Reaching this message means an accessor ran in one of those three phases.
/// `Ended` exists so that a re-entry after the pass answered cannot be one of
/// them: `End` releases into `None`, so without a separate terminal phase a
/// second poll would re-enter `End` and `expect` on an empty `Option`.
const SCHEDULER_PASS_EXPECT: &str = "the converter, order and census are adopted at the transition into \
H2WritePhase::Prepare and released at the first statement of \
H2WritePhase::End; only Start, Resume and Ended may observe them absent";
/// Where in its pass [`ConnectionH2::poll_write_target`] is, and the only
/// thing that tells a re-entry after a transmit apart from a first entry.
///
/// [`ConnectionH2::poll_write_target`]: super::h2::ConnectionH2::poll_write_target
pub
/// The `let` bindings of one `write_streams` pass, in the order the pre-image
/// declared them, plus the phase and the three late-initialised values the
/// inversion has to carry across the poll/handle boundary.
/// A tripwire, not the guarantee.
///
/// The guarantee that the converter's three pooled buffers reach
/// `HpackState` again is structural: between
/// `Self::adopt_scheduler_pass` and the first statement of
/// `H2WritePhase::End`, `ConnectionH2::poll_write_target` has exactly ONE
/// `return` — the `Transmit` yield — and `H2Shell::write_streams`' drive
/// loop answers every `Transmit` and leaves only on `Done` or `Finalize`. So
/// no pass can end while `Self::converter` is `Some`.
///
/// This catches the one way that could stop being true: a future caller of the
/// now-`pub` `poll_write_target` that stops driving after a `Transmit` — a
/// caller this crate no longer enumerates by reading its own call sites. The
/// other way — a caller that polls once MORE after the pass answered — is
/// closed by `H2WritePhase::Ended` instead, and needs no tripwire because
/// the converter is already `None` by then.
/// Dropping the converter there loses the buffers permanently and silently —
/// `HpackState` simply re-grows three `Vec`s on every pass from then on, with
/// no error and no metric. `debug_assert!` puts that in the test suite's reach
/// instead; `thread::panicking()` keeps a real test failure legible rather
/// than aborting the process on a double panic.