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
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Private driver for decoding one codec value from buffered input.
use core::num::NonZeroUsize;
use std::io::{
Error,
ErrorKind,
Result,
};
use qubit_io::{
BufferedInput,
Input,
};
use crate::{
Codec,
DecodeFailure,
};
use qubit_utils::allocation_error;
/// Drives one codec decode operation against persistent buffered input.
pub(super) struct CodecDecodeDriver<'a, I>
where
I: Input,
I::Item: Copy + Default,
{
/// Buffered unit input shared with the public adapter.
input: &'a mut BufferedInput<I>,
}
impl<'a, I> CodecDecodeDriver<'a, I>
where
I: Input,
I::Item: Copy + Default,
{
/// Creates a one-value decode driver over `input`.
#[inline(always)]
pub(super) const fn new(input: &'a mut BufferedInput<I>) -> Self {
Self { input }
}
/// Reads one decoded value after codec lifecycle reset completed.
pub(super) fn read_one<C, M>(
&mut self,
codec: &mut C,
map_error: &mut M,
) -> Result<C::Value>
where
C: Codec<Unit = I::Item>,
M: FnMut(C::DecodeError) -> Error,
{
let min_units_per_value = C::MIN_UNITS_PER_VALUE;
let max_units_per_value =
C::MAX_DECODE_UNITS_PER_VALUE.max(min_units_per_value);
self.input
.try_reserve_capacity(min_units_per_value)
.map_err(allocation_error)?;
loop {
let (available, end_of_input) = self.prepare_buffered_window(
min_units_per_value,
max_units_per_value,
)?;
let units = &self.input.unread()[..available];
debug_assert!(units.len() >= min_units_per_value);
let decode_result = unsafe {
// SAFETY: `min_units_per_value <= units.len()` guarantees
// the selected decode entry point's preconditions for this
// slice.
if end_of_input {
codec.decode_eof(units, 0)
} else {
codec.decode(units, 0)
}
};
match decode_result {
Ok((value, consumed)) => {
return self.accept(value, consumed, available);
}
Err(DecodeFailure::Incomplete {
source,
required_total,
}) => {
assert!(
required_total.get() <= C::MAX_DECODE_UNITS_PER_VALUE,
"Codec::decode incomplete required_total exceeded Codec::MAX_DECODE_UNITS_PER_VALUE",
);
if end_of_input {
let available = self.input.unread_len();
if required_total.get() <= available {
return Err(Error::new(
ErrorKind::InvalidData,
"codec reported incomplete input within available window",
));
}
// SAFETY: `available` is the current unread length.
unsafe {
self.input.consume(available);
}
return match source {
Some(source) => Err(map_error(source)),
None => Err(Error::new(
ErrorKind::UnexpectedEof,
"failed to decode complete value",
)),
};
}
if !self
.refill_after_incomplete(required_total, available)?
{
let available = self.input.unread_len();
let units = &self.input.unread()[..available];
let eof_result = unsafe {
// SAFETY: the original decode established that at
// least the codec minimum units remain available.
codec.decode_eof(units, 0)
};
match eof_result {
Ok((value, consumed)) => {
return self.accept(value, consumed, available);
}
Err(DecodeFailure::Invalid {
source,
consumed,
}) => {
return self.reject::<C, M>(
source, consumed, available, map_error,
);
}
Err(DecodeFailure::Incomplete {
source,
required_total,
}) => {
assert!(
required_total.get()
<= C::MAX_DECODE_UNITS_PER_VALUE,
"Codec::decode_eof incomplete required_total exceeded Codec::MAX_DECODE_UNITS_PER_VALUE",
);
if required_total.get() <= available {
return Err(Error::new(
ErrorKind::InvalidData,
"codec reported incomplete input within available window",
));
}
unsafe {
self.input.consume(available);
}
return match source {
Some(source) => Err(map_error(source)),
None => Err(Error::new(
ErrorKind::UnexpectedEof,
"failed to decode complete value",
)),
};
}
}
}
}
Err(DecodeFailure::Invalid { source, consumed }) => {
return self.reject::<C, M>(
source, consumed, available, map_error,
);
}
}
}
}
/// Prepares the buffered window for one codec decode attempt.
fn prepare_buffered_window(
&mut self,
min_units_per_value: usize,
max_units_per_value: usize,
) -> Result<(usize, bool)> {
let mut end_of_input = false;
let available = self.input.unread_len();
if available < min_units_per_value
&& !self.input.fill_until(min_units_per_value)?
{
end_of_input = true;
}
if self.input.unread_len() < min_units_per_value {
let available = self.input.unread_len();
// SAFETY: `available` is the current unread length.
unsafe {
self.input.consume(available);
}
return Err(Error::new(
ErrorKind::UnexpectedEof,
"failed to decode complete value",
));
}
if self.input.unread_len() < max_units_per_value
&& max_units_per_value <= self.input.capacity()
&& !self.input.fill_until(max_units_per_value)?
{
end_of_input = true;
}
Ok((
self.input.unread_len().min(max_units_per_value),
end_of_input,
))
}
/// Accepts a decoded value and consumes its source units.
fn accept<Value>(
&mut self,
value: Value,
consumed: NonZeroUsize,
available: usize,
) -> Result<Value> {
if consumed.get() > available {
return Err(Error::new(
ErrorKind::InvalidData,
"codec consumed units exceed unread window",
));
}
// SAFETY: The check above proves `consumed <= available`, and
// `available` came from the current unread window.
unsafe {
self.input.consume(consumed.get());
}
Ok(value)
}
/// Refills after the codec reports incomplete input.
fn refill_after_incomplete(
&mut self,
required_total: NonZeroUsize,
available: usize,
) -> Result<bool> {
let required_total = required_total.get();
if available >= required_total {
return Err(Error::new(
ErrorKind::InvalidData,
"codec reported incomplete input within available window",
));
}
self.input
.try_reserve_capacity(required_total)
.map_err(allocation_error)?;
self.input.fill_until(required_total)
}
/// Rejects invalid codec input and applies its consumption hint.
fn reject<C, M>(
&mut self,
source: C::DecodeError,
consumed: Option<NonZeroUsize>,
available: usize,
map_error: &mut M,
) -> Result<C::Value>
where
C: Codec<Unit = I::Item>,
M: FnMut(C::DecodeError) -> Error,
{
if let Some(consumed) = consumed {
if consumed.get() > available {
return Err(Error::new(
ErrorKind::InvalidData,
"decode error consumed units exceed unread window",
));
}
// SAFETY: The check above proves `consumed <= available`, and
// `available` came from the current unread window.
unsafe {
self.input.consume(consumed.get());
}
}
Err(map_error(source))
}
}