use std::{
fmt, io,
pin::Pin,
task::{Context, Poll},
};
use bytes::{Bytes, BytesMut};
use futures_io::{AsyncRead, AsyncWrite};
use hyper::rt::{Read as HyperRead, ReadBufCursor, Write as HyperWrite};
const READ_CHUNK_SIZE: usize = 128 * 1024;
pub(crate) trait Stream:
AsyncRead + AsyncWrite + HyperRead + HyperWrite + Send + Unpin
{
fn poll_read_buf(
self: Pin<&mut Self>,
context: &mut Context<'_>,
buffer: &mut BytesMut,
) -> Poll<io::Result<usize>>;
}
pub struct Io {
inner: Box<dyn Stream>,
}
impl Io {
#[must_use]
pub fn from_futures<S>(stream: S) -> Self
where
S: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
Self::from_stream(FuturesIo::new(stream))
}
pub(crate) fn from_stream<S>(stream: S) -> Self
where
S: Stream + 'static,
{
Self {
inner: Box::new(stream),
}
}
pub fn poll_read_buf(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
buffer: &mut BytesMut,
) -> Poll<io::Result<usize>> {
if buffer.len() == buffer.capacity() {
buffer.reserve(READ_CHUNK_SIZE);
}
Pin::new(&mut *self.inner).poll_read_buf(context, buffer)
}
}
impl fmt::Debug for Io {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.debug_struct("Io").finish_non_exhaustive()
}
}
impl AsyncRead for Io {
fn poll_read(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
buffer: &mut [u8],
) -> Poll<io::Result<usize>> {
AsyncRead::poll_read(Pin::new(&mut *self.inner), context, buffer)
}
}
impl AsyncWrite for Io {
fn poll_write(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
bytes: &[u8],
) -> Poll<io::Result<usize>> {
AsyncWrite::poll_write(Pin::new(&mut *self.inner), context, bytes)
}
fn poll_write_vectored(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
buffers: &[io::IoSlice<'_>],
) -> Poll<io::Result<usize>> {
AsyncWrite::poll_write_vectored(Pin::new(&mut *self.inner), context, buffers)
}
fn poll_flush(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<io::Result<()>> {
AsyncWrite::poll_flush(Pin::new(&mut *self.inner), context)
}
fn poll_close(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<io::Result<()>> {
AsyncWrite::poll_close(Pin::new(&mut *self.inner), context)
}
}
impl HyperRead for Io {
fn poll_read(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
buffer: ReadBufCursor<'_>,
) -> Poll<io::Result<()>> {
HyperRead::poll_read(Pin::new(&mut *self.inner), context, buffer)
}
}
impl HyperWrite for Io {
fn poll_write(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
bytes: &[u8],
) -> Poll<io::Result<usize>> {
HyperWrite::poll_write(Pin::new(&mut *self.inner), context, bytes)
}
fn poll_flush(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<io::Result<()>> {
HyperWrite::poll_flush(Pin::new(&mut *self.inner), context)
}
fn poll_shutdown(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<io::Result<()>> {
HyperWrite::poll_shutdown(Pin::new(&mut *self.inner), context)
}
fn is_write_vectored(&self) -> bool {
self.inner.is_write_vectored()
}
fn poll_write_vectored(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
buffers: &[io::IoSlice<'_>],
) -> Poll<io::Result<usize>> {
HyperWrite::poll_write_vectored(Pin::new(&mut *self.inner), context, buffers)
}
}
pub(crate) struct PrefixedIo<S> {
prefix: Bytes,
stream: S,
}
impl<S> PrefixedIo<S> {
pub(crate) fn new(prefix: Bytes, stream: S) -> Self {
Self { prefix, stream }
}
}
impl<S> AsyncRead for PrefixedIo<S>
where
S: AsyncRead + Unpin,
{
fn poll_read(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
buffer: &mut [u8],
) -> Poll<io::Result<usize>> {
if !self.prefix.is_empty() {
let length = self.prefix.len().min(buffer.len());
buffer[..length].copy_from_slice(&self.prefix.split_to(length));
return Poll::Ready(Ok(length));
}
Pin::new(&mut self.stream).poll_read(context, buffer)
}
}
impl<S> AsyncWrite for PrefixedIo<S>
where
S: AsyncWrite + Unpin,
{
fn poll_write(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
bytes: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.stream).poll_write(context, bytes)
}
fn poll_write_vectored(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
buffers: &[io::IoSlice<'_>],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.stream).poll_write_vectored(context, buffers)
}
fn poll_flush(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.stream).poll_flush(context)
}
fn poll_close(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.stream).poll_close(context)
}
}
struct FuturesIo<S> {
stream: S,
read_buffer: Vec<u8>,
}
impl<S> FuturesIo<S> {
fn new(stream: S) -> Self {
Self {
stream,
read_buffer: Vec::new(),
}
}
}
impl<S> Stream for FuturesIo<S>
where
S: AsyncRead + AsyncWrite + Send + Unpin,
{
fn poll_read_buf(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
buffer: &mut BytesMut,
) -> Poll<io::Result<usize>> {
let available = buffer.capacity().saturating_sub(buffer.len());
if available == 0 {
return Poll::Ready(Ok(0));
}
let read_size = available.min(READ_CHUNK_SIZE);
if self.read_buffer.len() < read_size {
self.read_buffer.resize(read_size, 0);
}
let this = &mut *self;
match Pin::new(&mut this.stream).poll_read(context, &mut this.read_buffer[..read_size]) {
Poll::Ready(Ok(read)) => {
buffer.extend_from_slice(&this.read_buffer[..read]);
Poll::Ready(Ok(read))
}
Poll::Ready(Err(error)) => Poll::Ready(Err(error)),
Poll::Pending => Poll::Pending,
}
}
}
impl<S> AsyncRead for FuturesIo<S>
where
S: AsyncRead + Unpin,
{
fn poll_read(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
buffer: &mut [u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.stream).poll_read(context, buffer)
}
}
impl<S> AsyncWrite for FuturesIo<S>
where
S: AsyncWrite + Unpin,
{
fn poll_write(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
bytes: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.stream).poll_write(context, bytes)
}
fn poll_write_vectored(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
buffers: &[io::IoSlice<'_>],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.stream).poll_write_vectored(context, buffers)
}
fn poll_flush(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.stream).poll_flush(context)
}
fn poll_close(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.stream).poll_close(context)
}
}
impl<S> HyperRead for FuturesIo<S>
where
S: AsyncRead + Unpin,
{
fn poll_read(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
mut buffer: ReadBufCursor<'_>,
) -> Poll<io::Result<()>> {
let read_size = buffer.remaining().min(READ_CHUNK_SIZE);
if read_size == 0 {
return Poll::Ready(Ok(()));
}
if self.read_buffer.len() < read_size {
self.read_buffer.resize(read_size, 0);
}
let this = &mut *self;
let read = match Pin::new(&mut this.stream)
.poll_read(context, &mut this.read_buffer[..read_size])
{
Poll::Ready(Ok(read)) => read,
Poll::Ready(Err(error)) => return Poll::Ready(Err(error)),
Poll::Pending => return Poll::Pending,
};
if read > read_size {
return Poll::Ready(Err(io::Error::new(
io::ErrorKind::InvalidData,
"asynchronous reader reported more bytes than its buffer",
)));
}
buffer.put_slice(&this.read_buffer[..read]);
Poll::Ready(Ok(()))
}
}
impl<S> HyperWrite for FuturesIo<S>
where
S: AsyncWrite + Unpin,
{
fn poll_write(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
bytes: &[u8],
) -> Poll<io::Result<usize>> {
AsyncWrite::poll_write(Pin::new(&mut self.stream), context, bytes)
}
fn poll_flush(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<io::Result<()>> {
AsyncWrite::poll_flush(Pin::new(&mut self.stream), context)
}
fn poll_shutdown(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<io::Result<()>> {
AsyncWrite::poll_close(Pin::new(&mut self.stream), context)
}
fn is_write_vectored(&self) -> bool {
true
}
fn poll_write_vectored(
mut self: Pin<&mut Self>,
context: &mut Context<'_>,
buffers: &[io::IoSlice<'_>],
) -> Poll<io::Result<usize>> {
AsyncWrite::poll_write_vectored(Pin::new(&mut self.stream), context, buffers)
}
}
#[cfg(test)]
mod tests {
use std::task::Waker;
use hyper::rt::ReadBuf;
use super::*;
struct ReadOnce(Option<&'static [u8]>);
impl AsyncRead for ReadOnce {
fn poll_read(
mut self: Pin<&mut Self>,
_context: &mut Context<'_>,
buffer: &mut [u8],
) -> Poll<io::Result<usize>> {
let data = self.0.take().unwrap_or_default();
buffer[..data.len()].copy_from_slice(data);
Poll::Ready(Ok(data.len()))
}
}
impl AsyncWrite for ReadOnce {
fn poll_write(
self: Pin<&mut Self>,
_: &mut Context<'_>,
bytes: &[u8],
) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(bytes.len()))
}
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
}
struct InvalidReader;
impl AsyncRead for InvalidReader {
fn poll_read(
self: Pin<&mut Self>,
_context: &mut Context<'_>,
buffer: &mut [u8],
) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(buffer.len() + 1))
}
}
#[test]
fn futures_reader_fills_hypers_buffer_through_initialized_storage() {
let mut reader = FuturesIo::new(ReadOnce(Some(b"direct read")));
let mut backing = [0; 32];
let mut buffer = ReadBuf::new(&mut backing);
let mut context = Context::from_waker(Waker::noop());
assert!(
HyperRead::poll_read(Pin::new(&mut reader), &mut context, buffer.unfilled()).is_ready()
);
assert_eq!(buffer.filled(), b"direct read");
}
#[test]
fn futures_reader_cannot_advance_past_hypers_buffer() {
let mut reader = FuturesIo::new(InvalidReader);
let mut backing = [0; 8];
let mut buffer = ReadBuf::new(&mut backing);
let mut context = Context::from_waker(Waker::noop());
let Poll::Ready(Err(error)) =
HyperRead::poll_read(Pin::new(&mut reader), &mut context, buffer.unfilled())
else {
panic!("invalid reader length must fail");
};
assert_eq!(error.kind(), io::ErrorKind::InvalidData);
assert!(buffer.filled().is_empty());
}
#[test]
fn buffered_reads_reserve_capacity_instead_of_reporting_eof() {
let mut reader = Io::from_futures(ReadOnce(Some(b"x")));
let mut bytes = BytesMut::with_capacity(4);
bytes.extend_from_slice(b"full");
let mut context = Context::from_waker(Waker::noop());
assert!(matches!(
Pin::new(&mut reader).poll_read_buf(&mut context, &mut bytes),
Poll::Ready(Ok(1))
));
assert_eq!(&bytes[..], b"fullx");
}
}