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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*******************************************************************************
*
* Copyright (c) 2026 Haixing Hu.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0.
*
******************************************************************************/
//! Reusable buffered converter engine.
use super::{
buffered_convert_hooks::BufferedConvertHooks,
buffered_decode_engine::BufferedDecodeEngine,
buffered_encode_engine::BufferedEncodeEngine,
convert_error_of::{
ConvertErrorOf,
ConvertProgressResult,
},
convert_state::ConvertState,
convert_step_result::ConvertStepResult,
encode_context::EncodeContext,
encode_step::EncodeStep,
finish_error::FinishError,
pending_encode_step::PendingEncodeStep,
pending_value::PendingValue,
pending_value_slot::PendingValueSlot,
};
use crate::{
CapacityError,
Codec,
codec::assert_unit_bounds,
};
/// Reusable buffered conversion engine.
///
/// The engine owns reusable buffered decode and encode engines plus a small
/// conversion-level hook object. It keeps common converter control flow private:
/// index validation, pending-value retention, pending flush, decode-error
/// policy dispatch, encode planning, output-capacity checks, and progress
/// reporting.
///
/// `BufferedConvertEngine` is intentionally batch-oriented. Its public
/// `transcode` method drives a source/output buffer loop and reuses the same
/// unchecked codec and hook primitives as [`crate::BufferedDecodeEngine`] and
/// [`crate::BufferedEncodeEngine`]. It does not call one-value public
/// transcoders in the hot path.
///
/// # Type Parameters
///
/// - `D`: Source-side decoder codec.
/// - `E`: Target-side encoder codec.
/// - `H`: Conversion-level policy hooks.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct BufferedConvertEngine<D, E, H>
where
D: Codec,
E: Codec<Value = D::Value>,
H: BufferedConvertHooks<D, E>,
{
/// Source-side buffered decoder engine.
decode_engine: BufferedDecodeEngine<D, H::DecodeHooks>,
/// Target-side buffered encoder engine.
encode_engine: BufferedEncodeEngine<E, H::EncodeHooks>,
/// Conversion-level policy hooks.
hooks: H,
/// Decoded value waiting for target output capacity.
pending: PendingValueSlot<D::Value>,
}
impl<D, E, H> BufferedConvertEngine<D, E, H>
where
D: Codec,
E: Codec<Value = D::Value>,
H: BufferedConvertHooks<D, E>,
{
/// Creates a buffered converter engine.
///
/// The supplied conversion hooks create the internal decode and encode hook
/// instances. This keeps codec-specific hook initialization with the
/// conversion policy instead of requiring those hook types to implement
/// [`Default`].
///
/// # Parameters
///
/// - `decoder`: Low-level codec used for source decoding.
/// - `encoder`: Low-level codec used for target encoding.
/// - `hooks`: Conversion-level policy hooks.
///
/// # Returns
///
/// Returns a buffered converter engine.
#[must_use]
#[inline]
pub fn new(decoder: D, encoder: E, hooks: H) -> Self {
let decode_hooks = hooks.create_decode_hooks(&decoder, &encoder);
let encode_hooks = hooks.create_encode_hooks(&decoder, &encoder);
Self::from_parts(decoder, encoder, hooks, decode_hooks, encode_hooks)
}
/// Builds the engine from already-created component hooks.
///
/// # Type Parameters
///
/// - `D`: Source-side decoder codec.
/// - `E`: Target-side encoder codec.
/// - `H`: Conversion-level policy hooks.
///
/// # Parameters
///
/// - `decoder`: Low-level decode codec.
/// - `encoder`: Low-level encode codec.
/// - `hooks`: Conversion-level hook aggregator.
/// - `decode_hooks`: Decode hooks instance created from `hooks`.
/// - `encode_hooks`: Encode hooks instance created from `hooks`.
///
/// # Returns
///
/// Returns an engine assembled from the provided codecs and hooks.
#[inline(always)]
const fn from_parts(
decoder: D,
encoder: E,
hooks: H,
decode_hooks: H::DecodeHooks,
encode_hooks: H::EncodeHooks,
) -> Self {
Self {
decode_engine: BufferedDecodeEngine::new(decoder, decode_hooks),
encode_engine: BufferedEncodeEngine::new(encoder, encode_hooks),
hooks,
pending: PendingValueSlot::empty(),
}
}
/// Returns the source-side decode codec.
///
/// # Returns
///
/// Returns the reference to the internal decode codec.
#[must_use]
#[inline(always)]
fn decode_codec(&self) -> &D {
&self.decode_engine.codec
}
/// Returns the target-side encode codec.
///
/// # Returns
///
/// Returns the reference to the internal encode codec.
#[must_use]
#[inline(always)]
fn encode_codec(&self) -> &E {
&self.encode_engine.codec
}
/// Returns an upper bound for target units produced from `input_len` units.
#[must_use = "capacity planning can fail on overflow"]
pub fn max_output_len(&self, input_len: usize) -> Result<usize, CapacityError> {
let pending_units = self.pending_output_len()?;
let decoded_values = self.decode_engine.max_output_len(input_len)?;
let converted_units = self.encode_engine.max_output_len(decoded_values)?;
pending_units
.checked_add(converted_units)
.ok_or(CapacityError::OutputLengthOverflow)
}
/// Returns the maximum target units emitted by finishing retained state.
#[must_use = "capacity planning can fail on overflow"]
pub fn max_finish_output_len(&self) -> Result<usize, CapacityError> {
let pending_units = self.pending_output_len()?;
let decoder_finish_values = self.decode_engine.max_finish_output_len();
let decoder_finish_units = self.encode_engine.max_output_len(decoder_finish_values)?;
let encoder_finish_units = self.encode_engine.max_finish_output_len();
let pending_and_decoder = pending_units
.checked_add(decoder_finish_units)
.ok_or(CapacityError::OutputLengthOverflow)?;
pending_and_decoder
.checked_add(encoder_finish_units)
.ok_or(CapacityError::OutputLengthOverflow)
}
/// Converts source units into target units.
///
/// # Parameters
///
/// - `input`: Complete input unit slice visible to the converter.
/// - `input_index`: Absolute input index where conversion starts.
/// - `output`: Complete output unit slice visible to the converter.
/// - `output_index`: Absolute output index where writing starts.
///
/// # Returns
///
/// Returns conversion progress.
///
/// # Errors
///
/// Returns hook errors when indices are invalid or concrete conversion fails.
/// Invalid output indices are reported through the encode-side error path.
pub fn transcode(
&mut self,
input: &[D::Unit],
input_index: usize,
output: &mut [E::Unit],
output_index: usize,
) -> ConvertProgressResult<D, E, H> {
if input_index > input.len() {
return Err(self
.hooks
.invalid_input_index(self.decode_codec(), input_index, input.len()));
}
if output_index > output.len() {
let error = self.encode_engine.invalid_output_index(output_index, output.len());
return Err(self.hooks.map_encode_error(error));
}
assert_unit_bounds::<D>(self.decode_codec());
assert_unit_bounds::<E>(self.encode_codec());
let mut state = ConvertState::new(input, input_index, output, output_index);
// A retained decoded value must be written before consuming more input,
// otherwise callers could observe output reordered across buffer turns.
if let Some(progress) = self.drain_pending(&mut state)? {
return Ok(progress);
}
while state.has_input() {
let previous_read = state.read();
// Each hot-path step decodes one source value and immediately tries
// to encode it, preserving backpressure at the target output.
if let Some(progress) = self.convert_next(&mut state)? {
return Ok(progress);
}
debug_assert!(
state.read() > previous_read,
"BufferedConvertEngine conversion step must consume input or stop",
);
}
Ok(state.complete_progress())
}
/// Finishes retained output after EOF.
///
/// Finalization drains a pending decoded value first, then lets the
/// source-side decode hooks emit final values, encodes those values through
/// the target-side encode hooks, and finally finishes target-side encode hook
/// state. The decode-finish value buffer used for this cold path requires
/// `D::Value: Default`; the normal `transcode` loop does not.
///
/// # Parameters
///
/// - `output`: Complete output unit slice visible to the converter.
/// - `output_index`: Absolute output index where writing starts.
///
/// # Returns
///
/// Returns the number of target units written during finalization.
///
/// # Errors
///
/// Returns [`FinishError`] when capacity planning overflows, when the caller
/// provides invalid or insufficient output capacity, or when hook
/// finalization fails.
pub fn finish(
&mut self,
output: &mut [E::Unit],
output_index: usize,
) -> Result<usize, FinishError<ConvertErrorOf<D, E, H>>>
where
D::Value: Default,
{
assert_unit_bounds::<D>(self.decode_codec());
assert_unit_bounds::<E>(self.encode_codec());
let required = self.max_finish_output_len().map_err(FinishError::capacity)?;
FinishError::ensure_output_capacity(output.len(), output_index, required)?;
let empty_input: &[D::Unit] = &[];
let mut state = ConvertState::new(empty_input, 0, output, output_index);
// Finish keeps the same priority as transcode: output any retained
// decoded value before asking source-side hooks for final values.
if self.drain_pending(&mut state).map_err(FinishError::source)?.is_some() {
unreachable!("converter finish bound must reserve space for pending values");
}
// Source-side finish may emit one or more final values. Drain them into
// the target encoder before finishing target-side hook state.
self.drain_decoder_finish(&mut state)?;
let output_cursor = state.output_cursor();
let written = self
.encode_engine
.finish(state.output_mut(), output_cursor)
.map_err(|error| error.map_source(|error| self.hooks.map_encode_error(error)))?;
state.advance_output(written);
Ok(state.written())
}
/// Resets hook-owned and component-owned state.
///
/// # Parameters
///
/// - `self`: Converter instance whose retained state is cleared.
///
/// # Returns
///
/// Returns unit `()`.
#[inline(always)]
pub fn reset(&mut self) {
self.pending.clear();
self.decode_engine.reset();
self.encode_engine.reset();
self.hooks.reset();
}
/// Converts one value from the current state cursors.
#[inline]
fn convert_next(&mut self, state: &mut ConvertState<'_, D::Unit, E::Unit>) -> ConvertStepResult<D, E, H> {
let step = self
.decode_engine
.decode_step(state.input(), state.decode_context())
.map_err(|error| self.hooks.map_decode_error(error))?;
step.apply_to_convert_state(state, |pending, state| self.encode_pending(pending, state))
}
/// Returns the output bound for the retained pending value.
#[inline(always)]
fn pending_output_len(&self) -> Result<usize, CapacityError> {
self.pending.max_output_len(&self.encode_engine)
}
/// Writes a retained decoded value before new input is consumed.
#[inline]
fn drain_pending(&mut self, state: &mut ConvertState<'_, D::Unit, E::Unit>) -> ConvertStepResult<D, E, H> {
let Some(pending) = self.pending.take() else {
return Ok(None);
};
self.encode_pending(pending, state)
}
/// Drains source-side decode finish output and encodes emitted final values.
fn drain_decoder_finish(
&mut self,
state: &mut ConvertState<'_, D::Unit, E::Unit>,
) -> Result<(), FinishError<ConvertErrorOf<D, E, H>>>
where
D::Value: Default,
{
let value_count = self.decode_engine.max_finish_output_len();
let mut decoded: Vec<D::Value> = (0..value_count).map(|_| D::Value::default()).collect();
let written = self
.decode_engine
.finish(&mut decoded, 0)
.map_err(|error| error.map_source(|error| self.hooks.map_decode_error(error)))?;
for value in decoded.into_iter().take(written) {
let pending = PendingValue::new(value, 0);
if self
.encode_pending(pending, state)
.map_err(FinishError::source)?
.is_some()
{
unreachable!("converter finish bound must reserve space for decode finish values");
}
}
Ok(())
}
/// Encodes one pending value and applies output/pending state changes.
#[inline]
fn encode_pending(
&mut self,
pending: PendingValue<D::Value>,
state: &mut ConvertState<'_, D::Unit, E::Unit>,
) -> ConvertStepResult<D, E, H> {
let input_index = pending.input_index();
let output_index = state.output_cursor();
let context = EncodeContext {
input_value: pending.value(),
input_index,
output: state.output_mut(),
output_index,
};
let step = self
.encode_engine
.encode_step(context)
.map_err(|error| self.hooks.map_encode_error(error))?;
let step = match step {
EncodeStep::Written { written } => PendingEncodeStep::written(written),
EncodeStep::NeedOutput { additional, available } => {
PendingEncodeStep::need_output(pending, additional, available)
}
};
Ok(self.pending.apply_pending_encode_step(step, state))
}
}
impl<D, E, H> Default for BufferedConvertEngine<D, E, H>
where
D: Codec + Default,
E: Codec<Value = D::Value> + Default,
H: BufferedConvertHooks<D, E> + Default,
{
/// Creates a default buffered converter engine.
///
/// # Returns
///
/// Returns a converter engine constructed from default codecs and hooks.
#[inline(always)]
fn default() -> Self {
Self::new(D::default(), E::default(), H::default())
}
}