qubit-codec 0.17.4

Core codec traits and buffer conversion primitives for Rust
Documentation
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Reusable buffered converter engine for codec-backed transcoding.
//!
//! Bridges a source [`crate::engine::TranscodeDecodeEngine`] and a target
//! [`crate::engine::TranscodeEncodeEngine`] into one unit-to-unit conversion
//! pipeline.

use core::num::NonZeroUsize;

use qubit_utils::try_reserve_vec;

use super::super::internal::convert_state::ConvertState;
use super::super::internal::encode_attempt::EncodeAttempt;
use super::super::internal::lifecycle_guard::LifecycleGuard;
use super::super::internal::pending_value::PendingValue;
use super::super::internal::pending_value_slot::PendingValueSlot;
use super::TranscodeDecodeHooks;
use super::TranscodeEncodeHooks;
use super::transcode_decode_engine::TranscodeDecodeEngine;
use super::transcode_encode_engine::TranscodeEncodeEngine;
use crate::CapacityError;
use crate::Codec;
use crate::TranscodeConvertError;
use crate::TranscodeConvertErrorOf;
use crate::TranscodeConverter;
use crate::TranscodeFailure;
use crate::TranscodeProgress;
use crate::Transcoder;
use crate::codec::assert_unit_bounds;

/// Adds two independent target-output capacity bounds.
///
/// # Parameters
///
/// - `first`: First independent output bound.
/// - `second`: Second independent output bound.
///
/// # Returns
///
/// The checked sum of the supplied target-unit bounds.
///
/// # Errors
///
/// Returns [`CapacityError::OutputLengthOverflow`] when the sum exceeds
/// `usize`.
#[inline]
fn add_convert_output_bounds(first: usize, second: usize) -> Result<usize, CapacityError> {
    first.checked_add(second).ok_or(CapacityError::OutputLengthOverflow)
}

/// Adds three independent target-output capacity bounds.
///
/// # Parameters
///
/// - `first`: First independent target-output bound.
/// - `second`: Second independent target-output bound.
/// - `third`: Third independent target-output bound.
///
/// # Returns
///
/// The checked sum of the supplied target-unit bounds.
///
/// # Errors
///
/// Returns [`CapacityError::OutputLengthOverflow`] when the sum exceeds
/// `usize`.
#[inline]
fn sum_convert_output_bounds(first: usize, second: usize, third: usize) -> Result<usize, CapacityError> {
    let partial = add_convert_output_bounds(first, second)?;
    add_convert_output_bounds(partial, third)
}

/// Asserts that a pre-reserved conversion phase did not need more output.
///
/// # Parameters
///
/// - `progress`: `None` after draining, or `Some` when output is still needed.
/// - `message`: Invariant explanation included in a panic.
///
/// # Panics
///
/// Panics with `message` if `progress` is `Some`.
#[inline]
fn assert_reserved_output_drained(progress: Option<TranscodeProgress>, message: &'static str) {
    assert!(progress.is_none(), "{message}");
}

/// Reusable buffered conversion engine for codec-backed converters.
///
/// The engine owns reusable buffered decode and encode engines. It keeps
/// common converter control flow private: index validation, pending-value
/// retention, finish draining, decode-error policy dispatch, encode attempts,
/// output-capacity checks, and [`crate::TranscodeStatus`] reporting.
///
/// Use this type to build a streaming converter over two one-value [`Codec`]
/// implementations that share the same logical value type. Each hot-path step
/// decodes one source unit sequence into a value, then immediately tries to
/// encode that value into the target output buffer. If the target buffer lacks
/// capacity, the decoded value is retained in an internal pending slot and
/// must be drained before more source input is consumed, preserving output
/// order across buffer turns.
///
/// `TranscodeConvertEngine` is intentionally batch-oriented. Its public
/// [`Self::transcode`] method drives a source/output buffer loop and reuses the
/// same unchecked codec and hook primitives as
/// [`crate::engine::TranscodeDecodeEngine`] and
/// [`crate::engine::TranscodeEncodeEngine`]. It does not call one-value public
/// transcoders in the hot path.
///
/// Values emitted by the source codec's decode reset or finish phase have no
/// source-unit position. The converter reports the placeholder index 0 for
/// their encode contexts and errors. This index cannot distinguish
/// lifecycle-generated values from a value decoded at source index 0. Error
/// phases describe the underlying codec operation: encoding such a value uses
/// the main encode path, even during converter reset or finish. Ordinary
/// decoded values retain their index in the input slice supplied to the
/// decode call that produced them. A pending value keeps that index across
/// calls; it is not rebased to a later input slice and is not a whole-stream
/// offset. The retained index may be outside the bounds of a later input
/// slice.
///
/// For strict codec-backed conversion with default decode and encode policies,
/// use [`crate::CodecTranscodeConverter`]. Use `TranscodeConvertEngine`
/// directly when either side needs custom malformed-input repair, encode
/// planning, skipped values, or finish-time output.
///
/// The engine follows the same lifecycle as [`crate::Transcoder`]:
/// `reset → transcode* → finish → reset`. Call [`Self::reset`] before starting
/// a new logical stream and [`Self::finish`] after EOF once any incomplete
/// source tail has been handled.
///
/// # Examples
///
/// ```rust
/// use core::convert::Infallible;
/// use core::num::NonZeroUsize;
///
/// use qubit_codec::Codec;
/// use qubit_codec::DecodeFailure;
/// use qubit_codec::TranscodeDecodeErrorOf;
/// use qubit_codec::TranscodeEncodeErrorOf;
/// use qubit_codec::TranscodeStatus;
/// use qubit_codec::engine::DecodeContext;
/// use qubit_codec::engine::EncodeContext;
/// use qubit_codec::engine::EncodeUnencodableAction;
/// use qubit_codec::engine::TranscodeConvertEngine;
/// use qubit_codec::engine::TranscodeDecodeHooks;
/// use qubit_codec::engine::TranscodeEncodeHooks;
///
/// #[derive(Clone, Copy)]
/// struct SourceCodec;
///
/// #[derive(Clone, Copy)]
/// struct TargetCodec;
///
/// impl Codec for SourceCodec {
///     type Value = u8;
///     type Unit = u8;
///     type DecodeError = Infallible;
///     type EncodeError = Infallible;
///
///     const MIN_UNITS_PER_VALUE: usize = 1;
///     const MAX_ENCODE_UNITS_PER_VALUE: usize = 1;
///     const MAX_DECODE_UNITS_PER_VALUE: usize = 1;
///
///     unsafe fn decode(
///         &mut self,
///         input: &[u8],
///         index: usize,
///     ) -> Result<(u8, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
///         Ok((input[index].wrapping_add(1), NonZeroUsize::MIN))
///     }
///
///     unsafe fn encode(
///         &mut self,
///         value: &u8,
///         output: &mut [u8],
///         index: usize,
///     ) -> Result<usize, Self::EncodeError> {
///         output[index] = *value;
///         Ok(1)
///     }
/// }
///
/// impl Codec for TargetCodec {
///     type Value = u8;
///     type Unit = u8;
///     type DecodeError = Infallible;
///     type EncodeError = Infallible;
///
///     const MIN_UNITS_PER_VALUE: usize = 1;
///     const MAX_ENCODE_UNITS_PER_VALUE: usize = 1;
///     const MAX_DECODE_UNITS_PER_VALUE: usize = 1;
///
///     unsafe fn decode(
///         &mut self,
///         input: &[u8],
///         index: usize,
///     ) -> Result<(u8, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
///         Ok((input[index], NonZeroUsize::MIN))
///     }
///
///     unsafe fn encode(
///         &mut self,
///         value: &u8,
///         output: &mut [u8],
///         index: usize,
///     ) -> Result<usize, Self::EncodeError> {
///         output[index] = *value;
///         Ok(1)
///     }
/// }
///
/// struct StrictDecodeHooks;
///
/// impl TranscodeDecodeHooks<SourceCodec> for StrictDecodeHooks {
///     fn handle_invalid_decode(
///         &mut self,
///         _codec: &mut SourceCodec,
///         error: &Infallible,
///         _consumed: Option<NonZeroUsize>,
///         _context: DecodeContext,
///     ) -> Result<qubit_codec::engine::DecodeInvalidAction<u8>, TranscodeDecodeErrorOf<SourceCodec>> {
///         match *error {}
///     }
/// }
///
/// struct StrictEncodeHooks;
///
/// impl TranscodeEncodeHooks<TargetCodec> for StrictEncodeHooks {
///     fn handle_unencodable_encode(
///         &mut self,
///         _codec: &mut TargetCodec,
///         _context: &EncodeContext<'_, u8>,
///     ) -> Result<EncodeUnencodableAction<u8>, TranscodeEncodeErrorOf<TargetCodec>> {
///         unreachable!("TargetCodec accepts every u8")
///     }
/// }
///
/// let mut engine = TranscodeConvertEngine::new(
///     SourceCodec,
///     TargetCodec,
///     StrictDecodeHooks,
///     StrictEncodeHooks,
/// );
/// let input = [1_u8, 2, 3];
/// let mut output = [0_u8; 2];
/// let mut reset_output = [];
/// engine.reset(&mut reset_output, 0)?;
///
/// let progress = engine.transcode(&input, 0, &mut output, 0)?;
/// match progress.status() {
///     TranscodeStatus::NeedOutput { .. } => {
///         assert_eq!(2, progress.written());
///         assert_eq!([2, 3], output);
///         // Drain `output[..progress.written()]`, then resume at
///         // `progress.read()` with fresh output capacity.
///     }
///     TranscodeStatus::Complete => unreachable!("output is intentionally short"),
///     TranscodeStatus::NeedInput { .. } => unreachable!("input is complete"),
/// }
/// # Ok::<(), qubit_codec::TranscodeConvertErrorOf<SourceCodec, TargetCodec>>(())
/// ```
///
/// # Type Parameters
///
/// - `D`: Source-side decoder codec.
/// - `E`: Target-side encoder codec.
/// - `DH`: Source-side decode hooks.
/// - `EH`: Target-side encode hooks.
#[derive(Debug)]
pub struct TranscodeConvertEngine<D, E, DH, EH>
where
    D: Codec,
    E: Codec<Value = D::Value>,
    DH: TranscodeDecodeHooks<D>,
    EH: TranscodeEncodeHooks<E>,
{
    /// Source-side buffered decoder engine.
    decode_engine: TranscodeDecodeEngine<D, DH>,
    /// Target-side buffered encoder engine.
    encode_engine: TranscodeEncodeEngine<E, EH>,
    /// Decoded value waiting for target output capacity.
    pending: PendingValueSlot<D::Value>,
    /// Guard for the `reset → transcode* → finish` lifecycle in every build
    /// profile. The converter owns its own guard rather than delegating to the
    /// inner decode/encode engines, because lifecycle events here describe the
    /// converter as a whole.
    lifecycle: LifecycleGuard,
}

impl<D, E, DH, EH> TranscodeConvertEngine<D, E, DH, EH>
where
    D: Codec,
    E: Codec<Value = D::Value>,
    DH: TranscodeDecodeHooks<D>,
    EH: TranscodeEncodeHooks<E>,
{
    /// Creates a buffered converter engine.
    ///
    /// The caller supplies decode hooks and encode hooks directly.
    ///
    /// # Parameters
    ///
    /// - `decoder`: Low-level codec used for source decoding.
    /// - `encoder`: Low-level codec used for target encoding.
    /// - `decode_hooks`: Decode-side policy hooks.
    /// - `encode_hooks`: Encode-side policy hooks.
    ///
    /// # Returns
    ///
    /// Returns a buffered converter engine.
    ///
    /// # Compile-Time Checks
    ///
    /// Fails to compile when either codec declares a zero decode unit bound or
    /// when [`Codec::MIN_UNITS_PER_VALUE`] exceeds
    /// [`Codec::MAX_DECODE_UNITS_PER_VALUE`].
    #[inline]
    #[must_use]
    pub fn new(decoder: D, encoder: E, decode_hooks: DH, encode_hooks: EH) -> Self {
        assert_unit_bounds::<D>();
        assert_unit_bounds::<E>();
        Self {
            decode_engine: TranscodeDecodeEngine::new(decoder, decode_hooks),
            encode_engine: TranscodeEncodeEngine::new(encoder, encode_hooks),
            pending: PendingValueSlot::empty(),
            lifecycle: LifecycleGuard::new(),
        }
    }

    /// Returns the source-side codec.
    ///
    /// # Returns
    ///
    /// Returns a shared reference to the decoder codec owned by this engine.
    #[inline]
    #[must_use]
    pub fn source_codec(&self) -> &D {
        self.decode_engine.codec()
    }

    /// Returns the source-side codec mutably.
    ///
    /// # Returns
    ///
    /// Returns a mutable reference to the decoder codec owned by this engine.
    #[inline]
    #[must_use]
    pub fn source_codec_mut(&mut self) -> &mut D {
        self.decode_engine.codec_mut()
    }

    /// Returns the target-side codec.
    ///
    /// # Returns
    ///
    /// Returns a shared reference to the encoder codec owned by this engine.
    #[inline]
    #[must_use]
    pub fn target_codec(&self) -> &E {
        self.encode_engine.codec()
    }

    /// Returns the target-side codec mutably.
    ///
    /// # Returns
    ///
    /// Returns a mutable reference to the encoder codec owned by this engine.
    #[inline]
    #[must_use]
    pub fn target_codec_mut(&mut self) -> &mut E {
        self.encode_engine.codec_mut()
    }

    /// Returns the decode hooks used by this engine.
    ///
    /// # Returns
    ///
    /// Returns a shared reference to the decode hooks owned by this engine.
    #[inline]
    #[must_use]
    pub const fn decode_hooks(&self) -> &DH {
        self.decode_engine.hooks()
    }

    /// Returns the decode hooks mutably.
    ///
    /// Mutating the returned hooks does not reset the converter or clear a
    /// pending value. The replacement hooks must continue to satisfy their
    /// global output-capacity contract for every reachable converter state.
    ///
    /// # Returns
    ///
    /// Returns a mutable reference to the decode hooks owned by this engine.
    #[inline]
    #[must_use]
    pub fn decode_hooks_mut(&mut self) -> &mut DH {
        self.decode_engine.hooks_mut()
    }

    /// Returns the encode hooks used by this engine.
    ///
    /// # Returns
    ///
    /// Returns a shared reference to the encode hooks owned by this engine.
    #[inline]
    #[must_use]
    pub const fn encode_hooks(&self) -> &EH {
        self.encode_engine.hooks()
    }

    /// Returns the encode hooks mutably.
    ///
    /// Mutating the returned hooks does not reset the converter or clear a
    /// pending value. The replacement hooks must continue to satisfy their
    /// global output-capacity contract for every reachable converter state.
    ///
    /// # Returns
    ///
    /// Returns a mutable reference to the encode hooks owned by this engine.
    #[inline]
    #[must_use]
    pub fn encode_hooks_mut(&mut self) -> &mut EH {
        self.encode_engine.hooks_mut()
    }

    /// Consumes the engine and returns its codecs and hooks.
    ///
    /// Any pending value and lifecycle state owned by the converter are
    /// discarded. Callers should use this only when no further conversion state
    /// needs to be preserved.
    ///
    /// # Returns
    ///
    /// Returns the source codec, target codec, decode hooks, and encode hooks.
    #[inline]
    #[must_use]
    pub fn into_parts(self) -> (D, E, DH, EH) {
        let Self {
            decode_engine,
            encode_engine,
            ..
        } = self;
        let (source, decode_hooks) = decode_engine.into_parts();
        let (target, encode_hooks) = encode_engine.into_parts();
        (source, target, decode_hooks, encode_hooks)
    }

    /// Returns an upper bound for target units produced from `input_len` units.
    ///
    /// The bound sums three parts: one possible retained pending value, the
    /// maximum decoded values from the source side, and the maximum target
    /// units for those values on the encode side. It covers only the streaming
    /// convert phase and remains valid even when no value is currently pending.
    /// Downstream converters must use this engine-level API for capacity
    /// planning instead of recomputing the bound from the source or target
    /// [`Codec`] constants.
    ///
    /// # Parameters
    ///
    /// - `input_len`: Number of source units the caller plans to convert.
    ///
    /// # Returns
    ///
    /// Returns a conservative upper bound, or a capacity error on arithmetic
    /// overflow.
    ///
    /// # Errors
    ///
    /// Returns [`CapacityError`] when a component bound or their sum overflows
    /// `usize`.
    pub fn max_transcode_output_len(&self, input_len: usize) -> Result<usize, CapacityError> {
        let pending_units = self.encode_engine.max_transcode_output_len(1)?;
        let decoded_values = self.decode_engine.max_transcode_output_len(input_len)?;
        let converted_units = self.encode_engine.max_transcode_output_len(decoded_values)?;
        add_convert_output_bounds(converted_units, pending_units)
    }

    /// Returns the global maximum target units emitted when resetting stream
    /// state.
    ///
    /// Covers decode-side reset values (encoded to target units) plus
    /// encode-side reset units. Most codecs are stateless and return `0`
    /// for [`Codec::MAX_DECODE_RESET_VALUES`]; in that case this equals the
    /// encode reset bound only.
    ///
    /// # Returns
    ///
    /// Returns the combined decode-reset and encode-reset output bound, or a
    /// capacity error on arithmetic overflow.
    ///
    /// # Errors
    ///
    /// Returns [`CapacityError`] when a component bound or their sum overflows
    /// `usize`.
    pub fn max_reset_output_len(&self) -> Result<usize, CapacityError> {
        let decode_reset_units = self
            .encode_engine
            .max_transcode_output_len(D::MAX_DECODE_RESET_VALUES)?;
        let encode_reset_units = E::MAX_ENCODE_RESET_UNITS;
        add_convert_output_bounds(decode_reset_units, encode_reset_units)
    }

    /// Returns the maximum target units emitted by finishing retained state.
    ///
    /// The bound covers one possible retained pending value, the global
    /// decode-side finish-value bound (encoded to target units), and the
    /// global encode-side finish-unit bound. It is independent of whether a
    /// pending value currently exists.
    ///
    /// # Returns
    ///
    /// Returns the combined pending, decode-finish, and encode-finish output
    /// bound, or a capacity error on arithmetic overflow.
    ///
    /// # Errors
    ///
    /// Returns [`CapacityError`] when a component bound or their sum overflows
    /// `usize`.
    pub fn max_finish_output_len(&self) -> Result<usize, CapacityError> {
        let pending_units = self.encode_engine.max_transcode_output_len(1)?;
        let decoder_finish_values = self.decode_engine.max_finish_output_len()?;
        let decoder_finish_units = self.encode_engine.max_transcode_output_len(decoder_finish_values)?;
        let encoder_finish_units = self.encode_engine.max_finish_output_len()?;
        sum_convert_output_bounds(pending_units, decoder_finish_units, encoder_finish_units)
    }

    /// Returns the maximum target units needed by a complete one-shot
    /// conversion.
    ///
    /// The returned bound covers conversion reset output, the streaming convert
    /// phase for `input_len` source units, and finish output. Its components
    /// are global across transient state. Higher-level complete conversion
    /// helpers should use this engine-level bound instead of recomputing
    /// capacity from the source or target codec constants, because decode
    /// and encode hooks may change streaming or finish output.
    ///
    /// # Parameters
    ///
    /// - `input_len`: Number of source units in the complete stream.
    ///
    /// # Returns
    ///
    /// Returns the complete-stream target-output bound, or a capacity error on
    /// arithmetic overflow.
    ///
    /// # Errors
    ///
    /// Returns [`CapacityError`] when a component bound or their sum overflows
    /// `usize`.
    pub fn max_total_output_len(&self, input_len: usize) -> Result<usize, CapacityError> {
        let reset = self.max_reset_output_len()?;
        let transcode = self.max_transcode_output_len(input_len)?;
        let finish = self.max_finish_output_len()?;
        sum_convert_output_bounds(reset, transcode, finish)
    }

    /// Clears retained conversion state, runs before-reset hooks, and emits
    /// stream-start encode output.
    ///
    /// Reset clears any retained pending value, resets the target encoder, then
    /// drains decode-side reset values through that reset encoder state. The
    /// caller must provide enough output capacity for
    /// [`Self::max_reset_output_len`].
    ///
    /// # 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 while resetting stream state.
    ///
    /// # Errors
    ///
    /// Returns a converter error if reset validation, target reset output
    /// emission, or reservation of the internal lifecycle value buffer fails.
    /// Capacity and index failures occur before converter state is changed.
    /// Once lifecycle execution starts, failure leaves the converter poisoned
    /// until a later reset succeeds.
    ///
    /// # Panics
    ///
    /// Panics when decode-reset values cannot be encoded within the capacity
    /// reserved by [`Self::max_reset_output_len`].
    pub fn reset(&mut self, output: &mut [E::Unit], output_index: usize) -> Result<usize, TranscodeConvertErrorOf<D, E>>
    where
        D::Value: Default,
    {
        let required = self.max_reset_output_len()?;
        TranscodeFailure::ensure_output_capacity(output.len(), output_index, required)?;
        self.lifecycle.on_reset_start();

        self.pending.clear();

        // Reset the target first because source-side reset values must be
        // encoded under the target's new-stream state. The reset bound reserves
        // space for both target-owned output and the encoded source-reset
        // values, so encode_pending should never report `NeedOutput` here.
        let empty_input: &[D::Unit] = &[];
        let mut state = ConvertState::new(empty_input, 0, output, output_index);
        let output_cursor = state.output_cursor();
        let encoder_written = self.encode_engine.reset(state.output_mut(), output_cursor)?;
        state.advance_output(encoder_written);
        self.drain_decoder_reset(&mut state)?;
        self.lifecycle.on_reset_success();
        Ok(state.written())
    }

    /// Converts source units into target units.
    ///
    /// The engine drains any retained pending value before consuming new input.
    /// Each loop iteration decodes one source value and immediately attempts to
    /// encode it. Conversion stops when the input tail is incomplete, when the
    /// output buffer cannot hold the next encoded value, or when the visible
    /// input is exhausted.
    ///
    /// # 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 describing input units consumed, target
    /// units written, and why conversion stopped.
    ///
    /// # Errors
    ///
    /// Returns [`TranscodeConvertError::Failure`] with
    /// [`TranscodeFailure::InvalidInputIndex`] for an invalid input index or
    /// [`TranscodeFailure::InvalidOutputIndex`] for an invalid output index.
    /// Concrete codec or hook conversion errors retain their decode-side or
    /// encode-side classification. Returns
    /// [`TranscodeFailure::TranscodeBeforeReset`] when the
    /// engine has not completed its first reset. Returns
    /// [`TranscodeFailure::TranscodeAfterFinish`] when the logical stream was
    /// already finished and has not been reset, or
    /// [`TranscodeFailure::LifecyclePoisoned`] when an earlier reset or finish
    /// failed after execution started.
    ///
    /// # Panics
    ///
    /// Panics when a codec or hook violates its declared decode or encode
    /// bounds, including a decode hook consuming beyond the available input,
    /// or when an encode hook supplies an unencodable replacement value.
    #[inline]
    pub fn transcode(
        &mut self,
        input: &[D::Unit],
        input_index: usize,
        output: &mut [E::Unit],
        output_index: usize,
    ) -> Result<TranscodeProgress, TranscodeConvertErrorOf<D, E>> {
        self.transcode_with_eof(input, input_index, output, output_index, false)
    }

    /// Converts source units after the caller has established end of input.
    ///
    /// # Parameters
    ///
    /// - `input`: Source units visible to this call.
    /// - `input_index`: Starting index in `input`.
    /// - `output`: Destination for target units.
    /// - `output_index`: Starting index in `output`.
    ///
    /// # Returns
    ///
    /// Consumed source units, written target units, and the stopping reason.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid indices, a missing reset, a finished or
    /// poisoned stream, or codec/hook conversion failure.
    ///
    /// # Panics
    ///
    /// Panics when a codec or hook violates its declared decode or encode
    /// bounds, including a decode hook consuming beyond the available input,
    /// or when an encode hook supplies an unencodable replacement value.
    #[inline]
    pub fn transcode_eof(
        &mut self,
        input: &[D::Unit],
        input_index: usize,
        output: &mut [E::Unit],
        output_index: usize,
    ) -> Result<TranscodeProgress, TranscodeConvertErrorOf<D, E>> {
        self.transcode_with_eof(input, input_index, output, output_index, true)
    }

    /// 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 [`Self::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 a converter error when output capacity checks fail, the
    /// internal lifecycle value buffer cannot be reserved, or hook
    /// finalization fails. Returns
    /// [`TranscodeFailure::FinishBeforeReset`] when the engine has not
    /// completed its first reset. Returns
    /// [`TranscodeFailure::FinishAfterFinish`] when the logical stream was
    /// already finished and has not been reset, or
    /// [`TranscodeFailure::LifecyclePoisoned`] when an earlier reset or finish
    /// failed after execution started. Capacity and index failures occur before
    /// finish execution and remain retryable; later failures poison the
    /// converter until reset succeeds. Once lifecycle execution starts,
    /// failure leaves the converter poisoned until a later reset succeeds.
    ///
    /// # Panics
    ///
    /// Panics when a retained pending value or decode-finish value cannot be
    /// encoded within the planned finish capacity.
    pub fn finish(
        &mut self,
        output: &mut [E::Unit],
        output_index: usize,
    ) -> Result<usize, TranscodeConvertErrorOf<D, E>>
    where
        D::Value: Default,
    {
        self.lifecycle.on_finish_attempt()?;
        let required = self.current_finish_output_len()?;
        TranscodeFailure::ensure_output_capacity(output.len(), output_index, required)?;
        self.lifecycle.on_finish_start();

        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.
        let progress = self.drain_pending(&mut state)?;
        assert_reserved_output_drained(progress, "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)?;
        state.advance_output(written);
        self.lifecycle.on_finish_success();
        Ok(state.written())
    }

    /// Runs a complete one-shot `reset -> transcode -> finish` conversion.
    ///
    /// The complete input is supplied as `input`, and output starts at index
    /// `0` in `output`. Callers that need subranges should slice their
    /// buffers before calling this method. Downstream one-shot converter
    /// helpers should call this engine method instead of reproducing the
    /// reset, transcode, and finish sequence themselves.
    ///
    /// # Parameters
    ///
    /// - `input`: Complete source unit slice.
    /// - `output`: Target unit slice for the whole converted stream.
    ///
    /// # Returns
    ///
    /// Returns the number of target units written.
    ///
    /// # Errors
    ///
    /// Returns framework errors for insufficient output, capacity overflow, or
    /// an incomplete EOF tail, and domain errors from reset, conversion, or
    /// finish.
    ///
    /// # Panics
    ///
    /// Panics if codec/hook lifecycle output exceeds its declared capacity
    /// bound.
    #[inline]
    pub fn transcode_complete_into(
        &mut self,
        input: &[D::Unit],
        output: &mut [E::Unit],
    ) -> Result<usize, TranscodeConvertErrorOf<D, E>>
    where
        D::Value: Default,
    {
        <Self as Transcoder>::transcode_complete_into(self, input, output)
    }

    /// Returns the finish-output bound for the converter's current pending
    /// state.
    ///
    /// Public capacity methods expose global bounds across every transient
    /// state. A concrete finish call can use this narrower checked bound while
    /// still relying on global component bounds for decoder and encoder
    /// finalization.
    ///
    /// # Returns
    ///
    /// Returns the current finish-output bound, or a capacity error on
    /// arithmetic overflow.
    ///
    /// # Errors
    ///
    /// Returns [`CapacityError`] when a component bound or their sum overflows
    /// `usize`.
    fn current_finish_output_len(&self) -> Result<usize, CapacityError> {
        let pending_units = self.pending.current_output_len(&self.encode_engine)?;
        let decoder_finish_values = self.decode_engine.max_finish_output_len()?;
        let decoder_finish_units = self.encode_engine.max_transcode_output_len(decoder_finish_values)?;
        let encoder_finish_units = self.encode_engine.max_finish_output_len()?;
        sum_convert_output_bounds(pending_units, decoder_finish_units, encoder_finish_units)
    }

    /// Runs one conversion step with an explicit end-of-input flag.
    ///
    /// The helper owns the shared lifecycle and cursor checks used by both
    /// streaming and EOF-aware public entry points.
    ///
    /// # Parameters
    ///
    /// - `input`: Source units visible to this call.
    /// - `input_index`: Starting index in `input`.
    /// - `output`: Destination for target units.
    /// - `output_index`: Starting index in `output`.
    /// - `end_of_input`: Whether the source is closed and EOF decode rules
    ///   apply.
    ///
    /// # Returns
    ///
    /// Consumed source units, written target units, and the stopping reason.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid indices, a missing reset, a finished or
    /// poisoned stream, or codec/hook conversion failure.
    fn transcode_with_eof(
        &mut self,
        input: &[D::Unit],
        input_index: usize,
        output: &mut [E::Unit],
        output_index: usize,
        end_of_input: bool,
    ) -> Result<TranscodeProgress, TranscodeConvertErrorOf<D, E>> {
        self.lifecycle.on_transcode()?;
        TranscodeFailure::ensure_transcode_indices(input.len(), input_index, output.len(), output_index)?;

        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);
        }

        let min_input_units =
            NonZeroUsize::new(D::MIN_UNITS_PER_VALUE).expect("Codec::MIN_UNITS_PER_VALUE is non-zero");
        let min_input_len = min_input_units.get();
        while state.has_input() {
            let available = state.available_input();
            if available < min_input_len && !end_of_input {
                return Ok(state.need_input_progress(min_input_units));
            }

            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, end_of_input)? {
                return Ok(progress);
            }
            debug_assert!(
                state.read() > previous_read,
                "TranscodeConvertEngine conversion step must consume input or stop",
            );
        }

        Ok(state.complete_progress())
    }

    /// Drains source-side decode reset output and encodes emitted reset
    /// values.
    ///
    /// Stateless decoders still call [`TranscodeDecodeEngine::reset`] so hook
    /// teardown side effects run even when no reset values are emitted.
    ///
    /// # Parameters
    ///
    /// - `state`: Current conversion cursors and output buffer.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` after all decode-reset values have been encoded.
    ///
    /// # Errors
    ///
    /// Returns a converter error when decode reset or encode reset handling
    /// fails.
    ///
    /// # Panics
    ///
    /// Panics when a decode-reset value cannot be encoded within the capacity
    /// reserved by [`Self::max_reset_output_len`].
    fn drain_decoder_reset(
        &mut self,
        state: &mut ConvertState<'_, D::Unit, E::Unit>,
    ) -> Result<(), TranscodeConvertErrorOf<D, E>>
    where
        D::Value: Default,
    {
        let value_count = D::MAX_DECODE_RESET_VALUES;
        if value_count == 0 {
            // Stateless decoder: still call decode_reset so codecs whose
            // hooks own teardown side effects (e.g. clearing accumulators)
            // run them. The empty slice is safe because the capacity check
            // inside `reset` accepts `required == 0` against any slice.
            self.decode_engine.reset(&mut [], 0)?;
            return Ok(());
        }
        // `D::Value: Default` is only consulted when the decoder declares
        // reset output. Stateless codecs never reach this branch.
        let mut reset_values = Vec::new();
        try_reserve_vec(&mut reset_values, value_count).map_err(|_| TranscodeFailure::allocation_failed())?;
        reset_values.resize_with(value_count, D::Value::default);
        let written = self.decode_engine.reset(&mut reset_values, 0)?;
        for value in reset_values.into_iter().take(written) {
            let pending = PendingValue::new(value, 0);
            let progress = self.encode_pending(pending, state)?;
            assert_reserved_output_drained(
                progress,
                "converter reset bound must reserve space for decode reset values",
            );
        }
        Ok(())
    }

    /// Converts one source value from the current state cursors.
    ///
    /// Decodes one value through the source engine, then immediately attempts
    /// to encode it through the target engine.
    ///
    /// # Parameters
    ///
    /// - `state`: Current conversion cursors and output buffer.
    /// - `end_of_input`: Whether to apply EOF-aware decode rules.
    ///
    /// # Returns
    ///
    /// Returns conversion progress when the step stops early, or `None` when
    /// the value was consumed and either encoded or skipped by policy.
    ///
    /// # Errors
    ///
    /// Returns a converter error when decode or encode handling fails.
    #[inline]
    fn convert_next(
        &mut self,
        state: &mut ConvertState<'_, D::Unit, E::Unit>,
        end_of_input: bool,
    ) -> Result<Option<TranscodeProgress>, TranscodeConvertErrorOf<D, E>> {
        let (outcome, pending) =
            self.decode_engine
                .decode_one(state.input(), state.decode_context(), end_of_input, PendingValue::new)?;
        if let Some(pending) = pending {
            self.pending.put(pending);
        }
        if let Some(progress) = state.apply_decode_outcome(outcome) {
            return Ok(Some(progress));
        }
        let Some(pending) = self.pending.take() else {
            return Ok(None);
        };
        self.encode_pending(pending, state)
    }

    /// Writes a retained decoded value before new input is consumed.
    ///
    /// # Parameters
    ///
    /// - `state`: Current conversion cursors and output buffer.
    ///
    /// # Returns
    ///
    /// Returns conversion progress when the pending value needs more output
    /// capacity, or `None` when no value was pending or the value was encoded
    /// or skipped by policy.
    ///
    /// # Errors
    ///
    /// Returns a converter error when encode handling fails.
    #[inline]
    fn drain_pending(
        &mut self,
        state: &mut ConvertState<'_, D::Unit, E::Unit>,
    ) -> Result<Option<TranscodeProgress>, TranscodeConvertErrorOf<D, E>> {
        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.
    ///
    /// When the decoder declares no finish output, still calls
    /// [`TranscodeDecodeEngine::finish`] so codec finish and hook teardown can
    /// run and fail even when zero values are emitted.
    ///
    /// # Parameters
    ///
    /// - `state`: Current conversion cursors and output buffer.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` after all decode-finish values have been encoded.
    ///
    /// # Errors
    ///
    /// Returns a converter error when decode finish or encode handling fails.
    ///
    /// # Panics
    ///
    /// Panics when a decode-finish value cannot be encoded within the
    /// capacity reserved by [`Self::max_finish_output_len`].
    fn drain_decoder_finish(
        &mut self,
        state: &mut ConvertState<'_, D::Unit, E::Unit>,
    ) -> Result<(), TranscodeConvertErrorOf<D, E>>
    where
        D::Value: Default,
    {
        let value_count = self.decode_engine.max_finish_output_len()?;
        if value_count == 0 {
            // Skip the Vec allocation when the decoder declares no finish
            // output. We still call finish() so that
            // codec.decode_finish and hooks.finish_hooks both run —
            // hooks may do validation or teardown (e.g. checksum
            // verification) that can fail even when emitting zero
            // values. Passing an empty slice is safe here because the capacity
            // check inside finish() accepts required == 0 against any slice.
            self.decode_engine.finish(&mut [], 0)?;
            return Ok(());
        }
        // D::Value: Default is required only when value_count > 0. The bound
        // remains on the method signature for the general case; stateless
        // codecs never reach this branch.
        let mut decoded = Vec::new();
        try_reserve_vec(&mut decoded, value_count).map_err(|_| TranscodeFailure::allocation_failed())?;
        decoded.resize_with(value_count, D::Value::default);
        let written = self.decode_engine.finish(&mut decoded, 0)?;
        for value in decoded.into_iter().take(written) {
            let pending = PendingValue::new(value, 0);
            let progress = self.encode_pending(pending, state)?;
            assert_reserved_output_drained(
                progress,
                "converter finish bound must reserve space for decode finish values",
            );
        }
        Ok(())
    }

    /// Encodes one pending value and applies output/pending state changes.
    ///
    /// When the target buffer lacks capacity, the value is put back into the
    /// pending slot and progress reports
    /// [`crate::TranscodeStatus::NeedOutput`].
    ///
    /// # Parameters
    ///
    /// - `pending`: Decoded value waiting for target output capacity.
    /// - `state`: Current conversion cursors and output buffer.
    ///
    /// # Returns
    ///
    /// Returns conversion progress when the value needs more output capacity,
    /// or `None` when the value was encoded or skipped by policy.
    ///
    /// # Errors
    ///
    /// Returns a converter error when encode hook handling fails.
    fn encode_pending(
        &mut self,
        pending: PendingValue<D::Value>,
        state: &mut ConvertState<'_, D::Unit, E::Unit>,
    ) -> Result<Option<TranscodeProgress>, TranscodeConvertErrorOf<D, E>> {
        let input_index = pending.input_index();
        let output_index = state.output_cursor();
        let attempt = EncodeAttempt::new(pending.value(), input_index, state.output_mut(), output_index);
        let outcome = match self.encode_engine.encode_one(attempt) {
            Ok(outcome) => outcome,
            Err(error) => {
                return Err(TranscodeConvertError::from_encode_error_with_value(
                    error,
                    pending.into_value(),
                ));
            }
        };
        let progress = state.apply_encode_outcome(outcome);
        if progress.is_some() {
            self.pending.put(pending);
        }
        Ok(progress)
    }
}

impl<D, E, DH, EH> Default for TranscodeConvertEngine<D, E, DH, EH>
where
    D: Codec + Default,
    E: Codec<Value = D::Value> + Default,
    DH: TranscodeDecodeHooks<D> + Default,
    EH: TranscodeEncodeHooks<E> + Default,
{
    /// Creates a default buffered converter engine.
    ///
    /// # Returns
    ///
    /// Returns a converter engine constructed from default codecs and hooks.
    fn default() -> Self {
        Self::new(D::default(), E::default(), DH::default(), EH::default())
    }
}

impl<D, E, DH, EH> Transcoder for TranscodeConvertEngine<D, E, DH, EH>
where
    D: Codec,
    E: Codec<Value = D::Value>,
    D::Value: Default,
    DH: TranscodeDecodeHooks<D>,
    EH: TranscodeEncodeHooks<E>,
{
    /// Source representation units accepted by the decoder.
    type Input = D::Unit;
    /// Target representation units emitted by the encoder.
    type Output = E::Unit;
    /// Source, target, contract, and capacity failures in the pipeline.
    type Error = TranscodeConvertErrorOf<D, E>;

    /// Returns an upper bound for target units produced from `input_len`
    /// units.
    ///
    /// # Parameters
    ///
    /// - `input_len`: Number of source units to include in capacity planning.
    ///
    /// # Returns
    ///
    /// The target-unit capacity bound, including the applicable retained state.
    ///
    /// # Errors
    ///
    /// Returns [`CapacityError`] when a component bound or their sum overflows
    /// `usize`.
    #[inline]
    fn max_transcode_output_len(&self, input_len: usize) -> Result<usize, CapacityError> {
        TranscodeConvertEngine::max_transcode_output_len(self, input_len)
    }

    /// Returns an upper bound for target units emitted by finishing retained
    /// state.
    ///
    /// # Returns
    ///
    /// The target-unit capacity bound, including the applicable retained state.
    ///
    /// # Errors
    ///
    /// Returns [`CapacityError`] when a component bound or their sum overflows
    /// `usize`.
    #[inline]
    fn max_finish_output_len(&self) -> Result<usize, CapacityError> {
        TranscodeConvertEngine::max_finish_output_len(self)
    }

    /// Returns an upper bound for target units emitted when resetting stream
    /// state.
    ///
    /// # Returns
    ///
    /// The target-unit capacity bound, including the applicable retained state.
    ///
    /// # Errors
    ///
    /// Returns [`CapacityError`] when a component bound or their sum overflows
    /// `usize`.
    #[inline]
    fn max_reset_output_len(&self) -> Result<usize, CapacityError> {
        TranscodeConvertEngine::max_reset_output_len(self)
    }

    /// Clears retained conversion state and emits target reset output.
    ///
    /// # Parameters
    ///
    /// - `output`: Destination for lifecycle output units.
    /// - `output_index`: Starting index in `output`.
    ///
    /// # Returns
    ///
    /// The number of lifecycle output units written.
    ///
    /// # Errors
    ///
    /// Returns the engine error for invalid indices, insufficient capacity,
    /// an invalid lifecycle transition, or codec/hook failure. See the inherent
    /// [`TranscodeConvertEngine::reset`] method for retry and poisoning
    /// semantics.
    ///
    /// # Panics
    ///
    /// Panics if a codec/hook output bound fails to reserve enough space for
    /// emitted values.
    #[inline]
    fn reset(&mut self, output: &mut [E::Unit], output_index: usize) -> Result<usize, TranscodeConvertErrorOf<D, E>> {
        TranscodeConvertEngine::reset(self, output, output_index)
    }

    /// Converts source units into target units.
    ///
    /// # Parameters
    ///
    /// - `input`: Source units visible to this call.
    /// - `input_index`: Starting index in `input`.
    /// - `output`: Destination for target units.
    /// - `output_index`: Starting index in `output`.
    ///
    /// # Returns
    ///
    /// Consumed source units, written target units, and the stopping reason.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid indices, a missing reset, a finished or
    /// poisoned stream, or codec/hook conversion failure.
    #[inline]
    fn transcode(
        &mut self,
        input: &[D::Unit],
        input_index: usize,
        output: &mut [E::Unit],
        output_index: usize,
    ) -> Result<TranscodeProgress, TranscodeConvertErrorOf<D, E>> {
        TranscodeConvertEngine::transcode(self, input, input_index, output, output_index)
    }

    /// Transcodes an input segment while marking it as the end of the stream.
    ///
    /// This forwarding implementation delegates to the engine's EOF-aware
    /// operation so the decoder can resolve trailing input using EOF rules.
    /// Call `finish` separately to drain retained lifecycle output.
    ///
    /// # Parameters
    ///
    /// - `input`: Source units visible to this call.
    /// - `input_index`: Starting index in `input`.
    /// - `output`: Destination for target units.
    /// - `output_index`: Starting index in `output`.
    ///
    /// # Returns
    ///
    /// Consumed source units, written target units, and the stopping reason.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid indices, a missing reset, a finished or
    /// poisoned stream, or codec/hook conversion failure.
    #[inline]
    fn transcode_eof(
        &mut self,
        input: &[D::Unit],
        input_index: usize,
        output: &mut [E::Unit],
        output_index: usize,
    ) -> Result<TranscodeProgress, TranscodeConvertErrorOf<D, E>> {
        TranscodeConvertEngine::transcode_eof(self, input, input_index, output, output_index)
    }

    /// Finishes retained converter output after EOF.
    ///
    /// # Parameters
    ///
    /// - `output`: Destination for lifecycle output units.
    /// - `output_index`: Starting index in `output`.
    ///
    /// # Returns
    ///
    /// The number of lifecycle output units written.
    ///
    /// # Errors
    ///
    /// Returns the engine error for invalid indices, insufficient capacity,
    /// an invalid lifecycle transition, or codec/hook failure. See the inherent
    /// [`TranscodeConvertEngine::finish`] method for retry and poisoning
    /// semantics.
    ///
    /// # Panics
    ///
    /// Panics if a codec/hook output bound fails to reserve enough space for
    /// emitted values.
    #[inline]
    fn finish(&mut self, output: &mut [E::Unit], output_index: usize) -> Result<usize, TranscodeConvertErrorOf<D, E>> {
        TranscodeConvertEngine::finish(self, output, output_index)
    }
}

impl<D, E, DH, EH> TranscodeConverter for TranscodeConvertEngine<D, E, DH, EH>
where
    D: Codec,
    E: Codec<Value = D::Value>,
    D::Value: Default,
    DH: TranscodeDecodeHooks<D>,
    EH: TranscodeEncodeHooks<E>,
{
    /// Source codec failures retained in conversion errors.
    type DecodeError = D::DecodeError;
    /// Target codec failures retained in conversion errors.
    type EncodeError = E::EncodeError;
    /// Shared logical value transferred between the two codecs.
    type Value = D::Value;
}