qubit-io-binary 0.8.6

Synchronous and asynchronous binary stream I/O adapters for Rust
Documentation
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================

//! Buffered reader for fixed-width binary values.

use core::marker::PhantomData;
use std::collections::TryReserveError;
use std::io::Result;
use std::io::SeekFrom;

use qubit_codec::BigEndian;
use qubit_codec::ByteOrder;
use qubit_codec::ByteOrderSpec;
use qubit_codec::LittleEndian;
use qubit_codec::NativeEndian;
use qubit_codec::TranscodeDecodeInput;
use qubit_codec_binary::BinaryCodec;
use qubit_io::Buffer;
use qubit_io::ByteInput;
use qubit_io::Input;
use qubit_io::Seekable;
use qubit_io::SeekableByteInput;

use super::internal::TranscodeDecodeInputExt;
use crate::util::MIN_CODEC_BUFFER_CAPACITY;
use crate::util::read_utf8_payload;
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
use crate::util::usize_from_u32_len;

/// Buffered reader for fixed-width binary values.
///
/// Scalar reads decode directly from the internal input buffer whenever enough
/// bytes are available, avoiding a per-value temporary byte buffer.
///
/// # Buffered state
///
/// This reader may prefetch bytes from the wrapped reader. As a result,
/// [`Self::inner`] can observe an underlying stream position ahead of the
/// logical position exposed by this wrapper.
///
/// # Type Parameters
///
/// - `R`: Underlying byte input.
/// - `O`: Compile-time byte-order specification.
///
/// # Examples
///
/// ```
/// use qubit_codec::BigEndian;
/// use qubit_io_binary::BufferedBinaryReader;
///
/// let mut reader = BufferedBinaryReader::<_, BigEndian>::new(std::io::Cursor::new([0x12, 0x34]));
/// assert_eq!(reader.read_u16()?, 0x1234);
/// # Ok::<(), std::io::Error>(())
/// ```
pub struct BufferedBinaryReader<R, O = BigEndian>
where
    R: ByteInput,
{
    /// Buffered codec input and wrapped reader.
    input: TranscodeDecodeInput<R>,
    /// Associates the selected byte order without storing a value.
    marker: PhantomData<fn() -> O>,
}

impl_input_methods!(BufferedBinaryReader<R, O> where R: ByteInput);
impl_seekable_methods!(BufferedBinaryReader<R, O> where R: SeekableByteInput);

impl<R, O> BufferedBinaryReader<R, O>
where
    R: ByteInput,
    O: ByteOrderSpec,
{
    /// Creates a buffered binary reader with the default buffer capacity.
    ///
    /// # Parameters
    ///
    /// - `inner`: Underlying byte input.
    ///
    /// # Returns
    ///
    /// Returns a buffered reader using byte order `O`.
    #[must_use]
    #[inline]
    pub fn new(inner: R) -> Self {
        Self {
            input: TranscodeDecodeInput::new(inner),
            marker: PhantomData,
        }
    }

    /// Creates a buffered binary reader with at least `capacity` bytes.
    ///
    /// # Parameters
    ///
    /// - `inner`: Underlying byte input.
    /// - `capacity`: Requested internal buffer capacity in bytes.
    ///
    /// # Returns
    ///
    /// Returns a buffered reader whose capacity also satisfies the largest
    /// supported codec payload.
    #[must_use]
    #[inline]
    pub fn with_capacity(inner: R, capacity: usize) -> Self {
        Self {
            input: TranscodeDecodeInput::with_capacity(inner, capacity.max(MIN_CODEC_BUFFER_CAPACITY)),
            marker: PhantomData,
        }
    }

    /// Tries to create a buffered binary reader with at least `capacity` bytes.
    ///
    /// # Parameters
    ///
    /// - `inner`: Underlying byte input to wrap.
    /// - `capacity`: Requested internal buffer capacity in bytes. The request
    ///   is raised when necessary to fit the largest supported codec payload.
    ///
    /// # Returns
    ///
    /// Returns a buffered reader on successful allocation. Its capacity
    /// satisfies both the request and the minimum required by the supported
    /// codecs.
    ///
    /// # Errors
    ///
    /// Returns a [`TryReserveError`] if the buffer capacity overflows or its
    /// allocation fails.
    #[inline]
    pub fn try_with_capacity(inner: R, capacity: usize) -> std::result::Result<Self, TryReserveError> {
        Ok(Self {
            input: TranscodeDecodeInput::try_with_capacity(inner, capacity.max(MIN_CODEC_BUFFER_CAPACITY))?,
            marker: PhantomData,
        })
    }

    /// Returns the byte order selected by this reader.
    ///
    /// # Returns
    ///
    /// Returns the compile-time byte order as a runtime value.
    #[must_use]
    #[inline(always)]
    pub const fn byte_order(&self) -> ByteOrder {
        O::ORDER
    }

    /// Returns a shared reference to the underlying reader.
    ///
    /// The underlying reader may already be positioned past unread bytes held
    /// in this wrapper's internal buffer.
    ///
    /// # Returns
    ///
    /// Returns the wrapped reader.
    #[must_use]
    #[inline(always)]
    pub const fn inner(&self) -> &R {
        self.input.inner()
    }

    /// Consumes this wrapper and preserves its unread buffered bytes.
    ///
    /// # Returns
    ///
    /// Returns the wrapped reader and the buffer whose [`Buffer::readable`]
    /// slice contains every prefetched byte not yet consumed logically. To
    /// continue the same logical stream, consume that slice before reading
    /// from the returned reader.
    #[inline(always)]
    #[must_use = "the returned inner reader and unread buffer must be handled"]
    pub fn into_parts(self) -> (R, Buffer<u8>) {
        self.input.into_parts()
    }
}

macro_rules! impl_value_read {
    ($order:ty, $method:ident, $ty:ty, $doc:literal) => {
        #[doc = $doc]
        #[doc = ""]
        #[doc = "# Returns"]
        #[doc = ""]
        #[doc = concat!("Returns the decoded `", stringify!($ty), "`.")]
        #[doc = ""]
        #[doc = "# Errors"]
        #[doc = ""]
        #[doc = "Returns an input error, including an unexpected-end-of-input \
                 error when the scalar is truncated."]
        #[inline(always)]
        pub fn $method(&mut self) -> Result<$ty> {
            type Codec = BinaryCodec<$ty, $order>;
            self.input.read_decoded::<Codec>()
        }
    };
}

macro_rules! impl_for_order {
    ($order:ty) => {
        impl<R> BufferedBinaryReader<R, $order>
        where
            R: ByteInput,
        {
            impl_value_read!($order, read_u8, u8, "Reads an unsigned 8-bit integer.");
            impl_value_read!($order, read_i8, i8, "Reads a signed 8-bit integer.");
            impl_value_read!($order, read_u16, u16, "Reads an unsigned 16-bit integer.");
            impl_value_read!($order, read_u32, u32, "Reads an unsigned 32-bit integer.");
            impl_value_read!($order, read_u64, u64, "Reads an unsigned 64-bit integer.");
            impl_value_read!($order, read_u128, u128, "Reads an unsigned 128-bit integer.");
            impl_value_read!($order, read_i16, i16, "Reads a signed 16-bit integer.");
            impl_value_read!($order, read_i32, i32, "Reads a signed 32-bit integer.");
            impl_value_read!($order, read_i64, i64, "Reads a signed 64-bit integer.");
            impl_value_read!($order, read_i128, i128, "Reads a signed 128-bit integer.");
            impl_value_read!($order, read_f32, f32, "Reads a 32-bit float.");
            impl_value_read!($order, read_f64, f64, "Reads a 64-bit float.");

            /// Reads a UTF-8 string prefixed by a `u16` byte length.
            ///
            /// # Parameters
            ///
            /// - `max_len`: Maximum UTF-8 payload length in bytes, excluding the length
            ///   prefix.
            ///
            /// # Returns
            ///
            /// Returns the decoded UTF-8 string.
            ///
            /// # Errors
            ///
            /// Returns an input error, or [`std::io::ErrorKind::InvalidData`]
            /// when the byte length exceeds `max_len` or the payload is not
            /// valid UTF-8. Also returns an allocation error if reserving the
            /// payload buffer fails.
            ///
            /// # Stream position
            ///
            /// If the decoded length exceeds `max_len`, the length prefix has been
            /// consumed logically, but the payload has not. If UTF-8 validation
            /// fails, the prefix and complete payload have been consumed logically.
            /// If reserving the payload buffer fails, the length prefix has been
            /// consumed logically, but the payload has not.
            /// The underlying input may already have prefetched payload bytes;
            /// its position is not the logical consumption position. These operations
            /// do not roll back; recover only at an outer framing boundary or a
            /// trusted seek checkpoint. See [`StringReadExt`](crate::StringReadExt)
            /// for the shared length-prefixed read contract.
            pub fn read_string_with_u16_len(&mut self, max_len: usize) -> Result<String> {
                let len = usize::from(self.read_u16()?);
                read_utf8_payload(self, len, max_len)
            }

            /// Reads a UTF-8 string prefixed by a `u32` byte length.
            ///
            /// # Parameters
            ///
            /// - `max_len`: Maximum UTF-8 payload length in bytes, excluding the length
            ///   prefix.
            ///
            /// # Returns
            ///
            /// Returns the decoded UTF-8 string.
            ///
            /// # Errors
            ///
            /// Returns an input error, or [`std::io::ErrorKind::InvalidData`]
            /// when the byte length does not fit `usize`, exceeds `max_len`,
            /// or the payload is not valid UTF-8. Also returns an allocation
            /// error if reserving the payload buffer fails.
            ///
            /// # Stream position
            ///
            /// If the decoded length exceeds `max_len`, the length prefix has been
            /// consumed logically, but the payload has not. If UTF-8 validation
            /// fails, the prefix and complete payload have been consumed logically.
            /// If reserving the payload buffer fails, the length prefix has been
            /// consumed logically, but the payload has not.
            /// The underlying input may already have prefetched payload bytes;
            /// its position is not the logical consumption position. These operations
            /// do not roll back; recover only at an outer framing boundary or a
            /// trusted seek checkpoint. See [`StringReadExt`](crate::StringReadExt)
            /// for the shared length-prefixed read contract.
            pub fn read_string_with_u32_len(&mut self, max_len: usize) -> Result<String> {
                #[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
                let len = self.read_u32()? as usize;
                #[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
                let len = usize_from_u32_len(self.read_u32()?)?;
                read_utf8_payload(self, len, max_len)
            }
        }
    };
}

impl_for_order!(BigEndian);
impl_for_order!(LittleEndian);
impl_for_order!(NativeEndian);

impl<R, O> Input for BufferedBinaryReader<R, O>
where
    R: ByteInput,
{
    /// The byte unit produced by this input adapter.
    type Item = u8;

    /// Reports that this adapter buffers input.
    ///
    /// # Returns
    ///
    /// Always returns `true`.
    #[inline(always)]
    fn is_buffered(&self) -> bool {
        true
    }

    /// Reads up to `count` bytes into an indexed output range.
    ///
    /// # Parameters
    ///
    /// - `output`: Destination slice.
    /// - `index`: First destination index.
    /// - `count`: Maximum number of bytes to read.
    ///
    /// # Returns
    ///
    /// Returns the number of bytes read.
    ///
    /// # Errors
    ///
    /// Returns an error reported while reading the buffered input.
    ///
    /// # Safety
    ///
    /// `index..index + count` must be a valid range within `output`.
    #[inline(always)]
    unsafe fn read_unchecked(&mut self, output: &mut [u8], index: usize, count: usize) -> Result<usize> {
        // SAFETY: The caller upholds the indexed destination range contract.
        unsafe { self.input.read_unchecked(output, index, count) }
    }
}

impl<R, O> Seekable for BufferedBinaryReader<R, O>
where
    R: SeekableByteInput,
{
    /// The byte unit used to report positions for this seekable adapter.
    type Unit = u8;

    /// Seeks relative to this reader's logical position. Relative seeks within
    /// the buffered window preserve unread bytes; successful seeks outside the
    /// window discard the old buffer. The returned position is logical.
    ///
    /// # Parameters
    ///
    /// - `position`: Target seek position.
    ///
    /// # Returns
    ///
    /// Returns the new logical stream position in bytes.
    ///
    /// # Errors
    ///
    /// Returns the seek error reported by the wrapped reader and preserves
    /// buffered bytes when seeking fails, including when an in-window seek
    /// cannot query the wrapped position.
    #[inline(always)]
    fn seek_to(&mut self, position: SeekFrom) -> Result<u64> {
        self.input.seek(position)
    }
}