qubit_io_binary/stream/buffered_leb128_reader.rs
1// =============================================================================
2// Copyright (c) 2026 Haixing Hu.
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6// Licensed under the Apache License, Version 2.0.
7// =============================================================================
8
9//! Buffered reader for LEB128-encoded values.
10
11use core::marker::PhantomData;
12use std::collections::TryReserveError;
13use std::io::Result;
14use std::io::SeekFrom;
15
16use qubit_codec::TranscodeDecodeInput;
17use qubit_codec_binary::Leb128Codec;
18use qubit_codec_binary::Leb128DecodePolicy;
19use qubit_codec_binary::NonStrict;
20use qubit_codec_binary::Strict;
21use qubit_io::Buffer;
22use qubit_io::Input;
23use qubit_io::Seekable;
24
25use crate::util::MIN_CODEC_BUFFER_CAPACITY;
26use crate::util::read_utf8_payload;
27#[cfg(not(target_pointer_width = "64"))]
28use crate::util::usize_from_u64_len;
29use super::internal::TranscodeDecodeInputExt;
30
31/// Buffered reader for LEB128 integers.
32///
33/// Values are decoded directly from the internal input buffer while the codec
34/// scans for the LEB128 terminating byte.
35///
36/// # Buffered state
37///
38/// This reader may prefetch bytes from the wrapped reader. As a result,
39/// [`Self::inner`] can observe an underlying stream position ahead of the
40/// logical position exposed by this wrapper.
41///
42/// # Target-width integers
43///
44/// `usize` and `isize` methods use the current Rust target's pointer width.
45/// Prefer fixed-width integer methods such as `read_u64` or `read_i64` for
46/// persistent files and cross-platform protocols.
47///
48/// # Type Parameters
49///
50/// - `R`: Underlying byte input.
51/// - `P`: LEB128 canonicality policy.
52pub struct BufferedLeb128Reader<R, P>
53where
54 R: Input<Item = u8>,
55{
56 /// Buffered codec input and wrapped reader.
57 input: TranscodeDecodeInput<R>,
58 /// Associates the selected decoding policy without storing a value.
59 marker: PhantomData<fn() -> P>,
60}
61
62impl<R, P> BufferedLeb128Reader<R, P>
63where
64 R: Input<Item = u8>,
65 P: Leb128DecodePolicy,
66{
67 /// Creates a buffered LEB128 reader with the default buffer capacity.
68 ///
69 /// # Parameters
70 ///
71 /// - `inner`: Underlying byte input.
72 ///
73 /// # Returns
74 ///
75 /// Returns a buffered reader using policy `P`.
76 #[must_use]
77 #[inline]
78 pub fn new(inner: R) -> Self {
79 Self {
80 input: TranscodeDecodeInput::new(inner),
81 marker: PhantomData,
82 }
83 }
84
85 /// Creates a buffered LEB128 reader with at least `capacity` bytes.
86 ///
87 /// # Parameters
88 ///
89 /// - `inner`: Underlying byte input.
90 /// - `capacity`: Requested internal buffer capacity in bytes.
91 ///
92 /// # Returns
93 ///
94 /// Returns a buffered reader whose capacity also satisfies the largest
95 /// supported codec payload.
96 #[must_use]
97 #[inline]
98 pub fn with_capacity(inner: R, capacity: usize) -> Self {
99 Self {
100 input: TranscodeDecodeInput::with_capacity(
101 inner,
102 capacity.max(MIN_CODEC_BUFFER_CAPACITY),
103 ),
104 marker: PhantomData,
105 }
106 }
107
108 /// Tries to create a buffered LEB128 reader with at least `capacity` bytes.
109 ///
110 /// # Errors
111 ///
112 /// Returns an allocation error when the requested buffer cannot be
113 /// allocated.
114 #[inline]
115 pub fn try_with_capacity(
116 inner: R,
117 capacity: usize,
118 ) -> std::result::Result<Self, TryReserveError> {
119 Ok(Self {
120 input: TranscodeDecodeInput::try_with_capacity(
121 inner,
122 capacity.max(MIN_CODEC_BUFFER_CAPACITY),
123 )?,
124 marker: PhantomData,
125 })
126 }
127
128 /// Returns whether this reader rejects non-canonical encodings.
129 ///
130 /// # Returns
131 ///
132 /// Returns `true` when policy `P` requires canonical encodings.
133 #[must_use]
134 #[inline(always)]
135 pub const fn is_strict(&self) -> bool {
136 P::STRICT
137 }
138
139 /// Returns a shared reference to the underlying reader.
140 ///
141 /// The underlying reader may already be positioned past unread bytes held
142 /// in this wrapper's internal buffer.
143 ///
144 /// # Returns
145 ///
146 /// Returns the wrapped reader.
147 #[must_use]
148 #[inline(always)]
149 pub const fn inner(&self) -> &R {
150 self.input.inner()
151 }
152
153 /// Consumes this wrapper and preserves its unread buffered bytes.
154 ///
155 /// # Returns
156 ///
157 /// Returns the wrapped reader and the buffer whose [`Buffer::readable`]
158 /// slice contains every prefetched byte not yet consumed logically. To
159 /// continue the same logical stream, consume that slice before reading
160 /// from the returned reader.
161 #[inline(always)]
162 #[must_use = "the returned inner reader and unread buffer must be handled"]
163 pub fn into_parts(self) -> (R, Buffer<u8>) {
164 self.input.into_parts()
165 }
166}
167
168macro_rules! impl_read_value {
169 ($policy:ty, $method:ident, $ty:ty, $doc:literal) => {
170 #[doc = $doc]
171 #[doc = ""]
172 #[doc = "# Returns"]
173 #[doc = ""]
174 #[doc = concat!("Returns the decoded `", stringify!($ty), "`.")]
175 #[doc = ""]
176 #[doc = "# Errors"]
177 #[doc = ""]
178 #[doc = "Returns an input error or an invalid-data error when the \
179 payload is malformed or violates the selected policy."]
180 #[inline(always)]
181 pub fn $method(&mut self) -> Result<$ty> {
182 type Codec = Leb128Codec<$ty, $policy>;
183
184 self.input.read_decoded::<Codec>()
185 }
186 };
187}
188
189macro_rules! impl_for_policy {
190 (
191 $policy:ty,
192 $read_usize:ident,
193 $read_u64:ident,
194 $read_utf8_string_usize:ident,
195 $read_utf8_string_u64:ident,
196 {
197 $(($method:ident, $ty:ty, $doc:literal)),* $(,)?
198 }
199 $(,)?
200 ) => {
201 impl<R> BufferedLeb128Reader<R, $policy>
202 where
203 R: Input<Item = u8>,
204 {
205 $(
206 impl_read_value!($policy, $method, $ty, $doc);
207 )*
208
209 /// Reads a UTF-8 string prefixed by an unsigned LEB128 byte length.
210 ///
211 /// The length prefix is decoded as usize, so this format is
212 /// target-width dependent. Prefer a fixed-width length prefix for
213 /// persistent files and cross-platform protocols.
214 ///
215 /// # Parameters
216 ///
217 /// - max_len: Maximum accepted UTF-8 payload length in bytes.
218 ///
219 /// # Returns
220 ///
221 /// Returns the decoded UTF-8 string.
222 ///
223 /// # Errors
224 ///
225 /// Returns an input or allocation error, or an invalid-data error
226 /// for a malformed or excessive length or invalid UTF-8.
227 #[inline]
228 pub fn $read_utf8_string_usize(
229 &mut self,
230 max_len: usize,
231 ) -> Result<String> {
232 let len = self.$read_usize()?;
233 read_utf8_payload(&mut self.input, len, max_len)
234 }
235
236 /// Reads a UTF-8 string prefixed by an unsigned LEB128 u64 byte
237 /// length.
238 ///
239 /// Prefer this method over the usize variant for persistent files
240 /// and cross-platform protocols because the length field is
241 /// independent of the current Rust target's pointer width.
242 ///
243 /// # Parameters
244 ///
245 /// - max_len: Maximum accepted UTF-8 payload length in bytes.
246 ///
247 /// # Returns
248 ///
249 /// Returns the decoded UTF-8 string.
250 ///
251 /// # Errors
252 ///
253 /// Returns an input, conversion, or allocation error, or an
254 /// invalid-data error for a malformed or excessive length or
255 /// invalid UTF-8.
256 #[inline]
257 pub fn $read_utf8_string_u64(
258 &mut self,
259 max_len: usize,
260 ) -> Result<String> {
261 let len = self.$read_u64()?;
262 #[cfg(target_pointer_width = "64")]
263 let len = len as usize;
264 #[cfg(not(target_pointer_width = "64"))]
265 let len = usize_from_u64_len(len)?;
266 read_utf8_payload(&mut self.input, len, max_len)
267 }
268 }
269 };
270}
271
272impl_for_policy!(
273 NonStrict,
274 read_usize_non_strict,
275 read_u64_non_strict,
276 read_utf8_string_usize_non_strict,
277 read_utf8_string_u64_non_strict,
278 {
279 (read_u8_non_strict, u8, "Reads an unsigned LEB128 u8."),
280 (read_u16_non_strict, u16, "Reads an unsigned LEB128 u16."),
281 (read_u32_non_strict, u32, "Reads an unsigned LEB128 u32."),
282 (read_u64_non_strict, u64, "Reads an unsigned LEB128 u64."),
283 (read_u128_non_strict, u128, "Reads an unsigned LEB128 u128."),
284 (read_usize_non_strict, usize, "Reads an unsigned LEB128 usize."),
285 (read_i8_non_strict, i8, "Reads a signed LEB128 i8."),
286 (read_i16_non_strict, i16, "Reads a signed LEB128 i16."),
287 (read_i32_non_strict, i32, "Reads a signed LEB128 i32."),
288 (read_i64_non_strict, i64, "Reads a signed LEB128 i64."),
289 (read_i128_non_strict, i128, "Reads a signed LEB128 i128."),
290 (read_isize_non_strict, isize, "Reads a signed LEB128 isize."),
291 },
292);
293impl_for_policy!(
294 Strict,
295 read_usize,
296 read_u64,
297 read_utf8_string_usize,
298 read_utf8_string_u64,
299 {
300 (read_u8, u8, "Reads an unsigned LEB128 u8."),
301 (read_u16, u16, "Reads an unsigned LEB128 u16."),
302 (read_u32, u32, "Reads an unsigned LEB128 u32."),
303 (read_u64, u64, "Reads an unsigned LEB128 u64."),
304 (read_u128, u128, "Reads an unsigned LEB128 u128."),
305 (read_usize, usize, "Reads an unsigned LEB128 usize."),
306 (read_i8, i8, "Reads a signed LEB128 i8."),
307 (read_i16, i16, "Reads a signed LEB128 i16."),
308 (read_i32, i32, "Reads a signed LEB128 i32."),
309 (read_i64, i64, "Reads a signed LEB128 i64."),
310 (read_i128, i128, "Reads a signed LEB128 i128."),
311 (read_isize, isize, "Reads a signed LEB128 isize."),
312 },
313);
314impl<R, P> Input for BufferedLeb128Reader<R, P>
315where
316 R: Input<Item = u8>,
317{
318 type Item = u8;
319
320 /// Reports that this adapter buffers input.
321 ///
322 /// # Returns
323 ///
324 /// Always returns `true`.
325 #[inline(always)]
326 fn is_buffered(&self) -> bool {
327 true
328 }
329
330 /// Reads up to `count` bytes into an indexed output range.
331 ///
332 /// # Parameters
333 ///
334 /// - `output`: Destination slice.
335 /// - `index`: First destination index.
336 /// - `count`: Maximum number of bytes to read.
337 ///
338 /// # Returns
339 ///
340 /// Returns the number of bytes read.
341 ///
342 /// # Errors
343 ///
344 /// Returns an error reported while reading the buffered input.
345 ///
346 /// # Safety
347 ///
348 /// `index..index + count` must be a valid range within `output`.
349 #[inline(always)]
350 unsafe fn read_unchecked(
351 &mut self,
352 output: &mut [u8],
353 index: usize,
354 count: usize,
355 ) -> Result<usize> {
356 // SAFETY: The caller upholds the indexed destination range contract.
357 unsafe { self.input.read_unchecked(output, index, count) }
358 }
359}
360
361impl<R, P> Seekable for BufferedLeb128Reader<R, P>
362where
363 R: Input<Item = u8> + Seekable<Unit = u8>,
364{
365 type Unit = u8;
366
367 /// Seeks the wrapped reader and discards buffered bytes after success.
368 ///
369 /// # Parameters
370 ///
371 /// - `position`: Target seek position.
372 ///
373 /// # Returns
374 ///
375 /// Returns the new physical stream position.
376 ///
377 /// # Errors
378 ///
379 /// Returns the seek error reported by the wrapped reader and preserves
380 /// buffered bytes when seeking fails.
381 #[inline(always)]
382 fn seek_to(&mut self, position: SeekFrom) -> Result<u64> {
383 self.input.seek(position)
384 }
385}