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
/*******************************************************************************
*
* Copyright (c) 2026 Haixing Hu.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0.
*
******************************************************************************/
//! Buffered decoder adapter backed by a low-level codec.
use super::{
BufferedDecoder,
BufferedTranscoder,
FinishError,
TranscodeProgress,
buffered_decode_engine::BufferedDecodeEngine,
codec_buffered_decode_hooks::CodecBufferedDecodeHooks,
};
use crate::{
CapacityError,
Codec,
CodecDecodeError,
};
/// Decodes encoded units into caller-provided value buffers by using a [`Codec`].
///
/// `CodecBufferedDecoder` is a policy-free bridge from the low-level unchecked
/// [`Codec`] contract to [`BufferedTranscoder`] and [`BufferedDecoder`]. It leaves
/// incomplete input tails in the caller-provided input slice; callers own
/// input-buffer refill and EOF incomplete-tail policy.
///
/// # Type Parameters
///
/// - `C`: Low-level codec used to decode values.
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct CodecBufferedDecoder<C> {
/// Common buffered decoding engine.
engine: BufferedDecodeEngine<C, CodecBufferedDecodeHooks>,
}
impl<C> CodecBufferedDecoder<C>
where
C: Codec,
{
/// Creates a buffered decoder backed by `codec`.
///
/// # Parameters
///
/// - `codec`: Low-level codec used to decode values.
///
/// # Returns
///
/// Returns a buffered decoder adapter for the supplied codec.
#[must_use]
#[inline(always)]
pub const fn new(codec: C) -> Self {
Self {
engine: BufferedDecodeEngine::new(codec, CodecBufferedDecodeHooks),
}
}
}
impl<C> BufferedTranscoder<C::Unit, C::Value> for CodecBufferedDecoder<C>
where
C: Codec,
{
type Error = CodecDecodeError<C::DecodeError>;
/// Returns an upper bound for decoded values produced from `input_len` units.
///
/// # Parameters
///
/// - `input_len`: Source units the caller plans to decode.
///
/// # Returns
///
/// Returns a conservative upper bound for decoded values.
#[inline(always)]
fn max_output_len(&self, input_len: usize) -> Result<usize, CapacityError> {
self.engine.max_output_len(input_len)
}
/// Returns the maximum values emitted by finishing internal state.
///
/// # Returns
///
/// Returns the number of values that may still be emitted by finishing state.
#[inline(always)]
fn max_finish_output_len(&self) -> Result<usize, CapacityError> {
Ok(self.engine.max_finish_output_len())
}
/// Resets hook-owned state.
///
/// # Returns
///
/// Returns unit `()`.
#[inline(always)]
fn reset(&mut self) {
self.engine.reset();
}
/// Decodes source units into logical values.
///
/// # Parameters
///
/// - `input`: Source unit slice.
/// - `input_index`: Absolute source index where decoding starts.
/// - `output`: Destination value slice.
/// - `output_index`: Absolute output value index where writing starts.
///
/// # Returns
///
/// Returns conversion progress for consumed and written counters.
///
/// # Errors
///
/// Returns a decode error when indices are invalid or when conversion fails
/// under hook policy.
#[inline(always)]
fn transcode(
&mut self,
input: &[C::Unit],
input_index: usize,
output: &mut [C::Value],
output_index: usize,
) -> Result<TranscodeProgress, Self::Error> {
self.engine.transcode(input, input_index, output, output_index)
}
/// Finishes internally retained output after EOF.
///
/// # Parameters
///
/// - `output`: Destination value slice for final retained values.
/// - `output_index`: Absolute output value index where writing starts.
///
/// # Returns
///
/// Returns the number of values written by finalization.
///
/// # Errors
///
/// Returns a finish error if finalization cannot complete.
#[inline(always)]
fn finish(&mut self, output: &mut [C::Value], output_index: usize) -> Result<usize, FinishError<Self::Error>> {
self.engine.finish(output, output_index)
}
}
impl<C> BufferedDecoder<C::Unit, C::Value> for CodecBufferedDecoder<C> where C: Codec {}