qubit-io 0.17.0

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

use std::pin::Pin;
use std::task::Context;
use std::task::Poll;

use tokio::io::AsyncRead;
use tokio::io::ReadBuf;

use crate::AsyncInput;
use crate::traits::normalize_async_error;
use crate::util::UncheckedSlice;

/// Adapts a Tokio [`AsyncRead`] value to Qubit's [`AsyncInput`].
///
/// # Type Parameters
///
/// - `T`: Tokio reader type.
#[must_use]
#[repr(transparent)]
pub struct TokioInput<T> {
    /// Tokio reader adapted as a Qubit input.
    inner: T,
}

impl<T> TokioInput<T> {
    /// Creates an adapter around a Tokio reader.
    ///
    /// # Parameters
    ///
    /// - `inner`: Tokio reader to adapt.
    ///
    /// # Returns
    ///
    /// Returns a Qubit input adapter that owns `inner`.
    #[inline(always)]
    pub const fn new(inner: T) -> Self {
        Self { inner }
    }

    /// Returns a shared reference to the wrapped reader.
    ///
    /// # Returns
    ///
    /// Returns the wrapped Tokio reader.
    #[inline(always)]
    #[must_use]
    pub const fn get_ref(&self) -> &T {
        &self.inner
    }

    /// Returns a mutable reference to the wrapped reader.
    ///
    /// # Returns
    ///
    /// Returns the wrapped Tokio reader with mutable access.
    #[inline(always)]
    #[must_use]
    pub const fn get_mut(&mut self) -> &mut T {
        &mut self.inner
    }

    /// Projects a pinned adapter to its pinned wrapped reader.
    ///
    /// # Returns
    ///
    /// Returns a pinned mutable reference to the wrapped reader without moving
    /// it.
    #[inline(always)]
    #[must_use]
    pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T> {
        // SAFETY: The projection does not move `inner`, and the transparent
        // adapter never exposes a way to replace a pinned inner value.
        unsafe { self.map_unchecked_mut(|this| &mut this.inner) }
    }

    /// Consumes the adapter and returns the wrapped reader.
    ///
    /// # Returns
    ///
    /// Returns the owned Tokio reader.
    #[inline(always)]
    #[must_use]
    pub fn into_inner(self) -> T {
        self.inner
    }
}

impl<T> AsyncInput for TokioInput<T>
where
    T: AsyncRead,
{
    /// Byte item produced by a Tokio reader.
    type Item = u8;

    /// Polls an indexed read through the wrapped Tokio reader.
    ///
    /// A zero-length request completes immediately without polling `inner`.
    ///
    /// # Parameters
    ///
    /// - `cx`: Task context used to register a wake-up.
    /// - `output`: Destination byte slice.
    /// - `index`: Starting destination index.
    /// - `count`: Maximum number of bytes to read.
    ///
    /// # Returns
    ///
    /// Returns [`Poll::Pending`] when the reader is not ready. A ready result
    /// contains the number of bytes read.
    ///
    /// # Errors
    ///
    /// Returns an I/O error reported by the wrapped reader. Invalid
    /// asynchronous error kinds are normalized to
    /// [`std::io::ErrorKind::InvalidData`].
    ///
    /// # Panics
    ///
    /// Panics in debug builds if the requested output range does not fit.
    ///
    /// # Safety
    ///
    /// The range `index..index + count` must be valid for `output`.
    #[inline]
    unsafe fn poll_read_unchecked(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        output: &mut [u8],
        index: usize,
        count: usize,
    ) -> Poll<std::io::Result<usize>> {
        if count == 0 {
            return Poll::Ready(Ok(0));
        }
        // SAFETY: The caller guarantees that the destination range is valid.
        let target = unsafe { UncheckedSlice::subslice_mut(output, index, count) };
        let mut buffer = ReadBuf::new(target);
        AsyncRead::poll_read(self.get_pin_mut(), cx, &mut buffer)
            .map(|result| result.map_err(normalize_async_error).map(|()| buffer.filled().len()))
    }
}