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
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Semantic marker trait for buffered converters.
use TranscodeConvertError;
use Transcoder;
/// Converts encoded units of one representation into encoded units of another.
///
/// `TranscodeConverter` refines [`Transcoder`] for implementations whose
/// input and output are both encoded unit streams. Any intermediate logical
/// values are implementation details of the concrete converter.
///
/// The trait adds no methods. It exists to make generic bounds distinguish
/// unit-to-unit conversion from value-to-unit encoding and unit-to-value
/// decoding.
///
/// # Examples
///
/// ```
/// use core::convert::Infallible;
/// use core::num::NonZeroUsize;
///
/// use qubit_codec::CapacityError;
/// use qubit_codec::TranscodeConvertError;
/// use qubit_codec::TranscodeConverter;
/// use qubit_codec::TranscodeProgress;
/// use qubit_codec::Transcoder;
///
/// #[derive(Default)]
/// struct ByteToWord;
/// # impl Transcoder for ByteToWord {
/// # type Input = u8;
/// # type Output = u16;
/// # type Error = TranscodeConvertError<Infallible, Infallible, u8>;
/// # fn max_transcode_output_len(&self, input_len: usize) -> Result<usize, CapacityError> { Ok(input_len) }
/// # fn reset(&mut self, _output: &mut [u16], _index: usize) -> Result<usize, Self::Error> { Ok(0) }
/// # fn finish(&mut self, _output: &mut [u16], _index: usize) -> Result<usize, Self::Error> { Ok(0) }
/// # fn transcode(&mut self, input: &[u8], input_index: usize, output: &mut [u16], output_index: usize) -> Result<TranscodeProgress, Self::Error> {
/// # let source = &input[input_index..];
/// # let target = &mut output[output_index..];
/// # let count = source.len().min(target.len());
/// # for (input, output) in source.iter().zip(target.iter_mut()) { *output = u16::from(*input); }
/// # if count == source.len() { Ok(TranscodeProgress::complete(count, count)) }
/// # else { Ok(TranscodeProgress::need_output(NonZeroUsize::MIN, count, count)) }
/// # }
/// # }
/// impl TranscodeConverter for ByteToWord {
/// type DecodeError = Infallible;
/// type EncodeError = Infallible;
/// type Value = u8;
/// }
///
/// let mut converter = ByteToWord::default();
/// let mut output = [0_u16; 2];
/// assert_eq!(converter.reset(&mut output, 0).unwrap(), 0);
/// let progress = converter.transcode(&[7, 9], 0, &mut output, 0).unwrap();
/// assert_eq!(output, [7, 9]);
/// assert_eq!(progress, TranscodeProgress::complete(2, 2));
/// assert_eq!(converter.finish(&mut output, progress.written()).unwrap(), 0);
/// ```