2020-09-24 17:26:03 -07:00
|
|
|
use bytes::Buf;
|
2020-09-08 09:12:32 +02:00
|
|
|
use futures_core::stream::Stream;
|
2020-04-02 23:18:52 +02:00
|
|
|
use pin_project_lite::pin_project;
|
|
|
|
|
use std::io;
|
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
use std::task::{Context, Poll};
|
2020-09-08 09:12:32 +02:00
|
|
|
use tokio::io::{AsyncBufRead, AsyncRead, ReadBuf};
|
2020-04-02 23:18:52 +02:00
|
|
|
|
|
|
|
|
pin_project! {
|
2020-09-08 09:12:32 +02:00
|
|
|
/// Convert a [`Stream`] of byte chunks into an [`AsyncRead`].
|
2020-04-02 23:18:52 +02:00
|
|
|
///
|
2020-09-08 09:12:32 +02:00
|
|
|
/// This type performs the inverse operation of [`ReaderStream`].
|
2020-04-02 23:18:52 +02:00
|
|
|
///
|
2020-09-08 09:12:32 +02:00
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use bytes::Bytes;
|
|
|
|
|
/// use tokio::io::{AsyncReadExt, Result};
|
|
|
|
|
/// use tokio_util::io::StreamReader;
|
|
|
|
|
/// # #[tokio::main]
|
|
|
|
|
/// # async fn main() -> std::io::Result<()> {
|
|
|
|
|
///
|
|
|
|
|
/// // Create a stream from an iterator.
|
|
|
|
|
/// let stream = tokio::stream::iter(vec![
|
|
|
|
|
/// Result::Ok(Bytes::from_static(&[0, 1, 2, 3])),
|
|
|
|
|
/// Result::Ok(Bytes::from_static(&[4, 5, 6, 7])),
|
|
|
|
|
/// Result::Ok(Bytes::from_static(&[8, 9, 10, 11])),
|
|
|
|
|
/// ]);
|
|
|
|
|
///
|
|
|
|
|
/// // Convert it to an AsyncRead.
|
|
|
|
|
/// let mut read = StreamReader::new(stream);
|
|
|
|
|
///
|
|
|
|
|
/// // Read five bytes from the stream.
|
|
|
|
|
/// let mut buf = [0; 5];
|
|
|
|
|
/// read.read_exact(&mut buf).await?;
|
|
|
|
|
/// assert_eq!(buf, [0, 1, 2, 3, 4]);
|
|
|
|
|
///
|
|
|
|
|
/// // Read the rest of the current chunk.
|
|
|
|
|
/// assert_eq!(read.read(&mut buf).await?, 3);
|
|
|
|
|
/// assert_eq!(&buf[..3], [5, 6, 7]);
|
|
|
|
|
///
|
|
|
|
|
/// // Read the next chunk.
|
|
|
|
|
/// assert_eq!(read.read(&mut buf).await?, 4);
|
|
|
|
|
/// assert_eq!(&buf[..4], [8, 9, 10, 11]);
|
|
|
|
|
///
|
|
|
|
|
/// // We have now reached the end.
|
|
|
|
|
/// assert_eq!(read.read(&mut buf).await?, 0);
|
|
|
|
|
///
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
/// # }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// [`AsyncRead`]: tokio::io::AsyncRead
|
|
|
|
|
/// [`Stream`]: tokio::stream::Stream
|
|
|
|
|
/// [`ReaderStream`]: crate::io::ReaderStream
|
2020-04-02 23:18:52 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct StreamReader<S, B> {
|
|
|
|
|
#[pin]
|
|
|
|
|
inner: S,
|
|
|
|
|
chunk: Option<B>,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 09:12:32 +02:00
|
|
|
impl<S, B, E> StreamReader<S, B>
|
2020-04-02 23:18:52 +02:00
|
|
|
where
|
2020-09-08 09:12:32 +02:00
|
|
|
S: Stream<Item = Result<B, E>>,
|
2020-04-02 23:18:52 +02:00
|
|
|
B: Buf,
|
2020-09-08 09:12:32 +02:00
|
|
|
E: Into<std::io::Error>,
|
2020-04-02 23:18:52 +02:00
|
|
|
{
|
2020-09-08 09:12:32 +02:00
|
|
|
/// Convert a stream of byte chunks into an [`AsyncRead`](tokio::io::AsyncRead).
|
|
|
|
|
///
|
|
|
|
|
/// The item should be a [`Result`] with the ok variant being something that
|
|
|
|
|
/// implements the [`Buf`] trait (e.g. `Vec<u8>` or `Bytes`). The error
|
|
|
|
|
/// should be convertible into an [io error].
|
|
|
|
|
///
|
|
|
|
|
/// [`Result`]: std::result::Result
|
|
|
|
|
/// [`Buf`]: bytes::Buf
|
|
|
|
|
/// [io error]: std::io::Error
|
|
|
|
|
pub fn new(stream: S) -> Self {
|
2020-04-02 23:18:52 +02:00
|
|
|
Self {
|
|
|
|
|
inner: stream,
|
|
|
|
|
chunk: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-08 09:12:32 +02:00
|
|
|
|
2020-04-02 23:18:52 +02:00
|
|
|
/// Do we have a chunk and is it non-empty?
|
|
|
|
|
fn has_chunk(self: Pin<&mut Self>) -> bool {
|
|
|
|
|
if let Some(chunk) = self.project().chunk {
|
|
|
|
|
chunk.remaining() > 0
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 09:12:32 +02:00
|
|
|
impl<S, B, E> AsyncRead for StreamReader<S, B>
|
2020-04-02 23:18:52 +02:00
|
|
|
where
|
2020-09-08 09:12:32 +02:00
|
|
|
S: Stream<Item = Result<B, E>>,
|
2020-04-02 23:18:52 +02:00
|
|
|
B: Buf,
|
2020-09-08 09:12:32 +02:00
|
|
|
E: Into<std::io::Error>,
|
2020-04-02 23:18:52 +02:00
|
|
|
{
|
|
|
|
|
fn poll_read(
|
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
|
cx: &mut Context<'_>,
|
2020-08-13 20:15:01 -07:00
|
|
|
buf: &mut ReadBuf<'_>,
|
|
|
|
|
) -> Poll<io::Result<()>> {
|
|
|
|
|
if buf.remaining() == 0 {
|
|
|
|
|
return Poll::Ready(Ok(()));
|
2020-04-02 23:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let inner_buf = match self.as_mut().poll_fill_buf(cx) {
|
|
|
|
|
Poll::Ready(Ok(buf)) => buf,
|
|
|
|
|
Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
|
|
|
|
|
Poll::Pending => return Poll::Pending,
|
|
|
|
|
};
|
2020-08-13 20:15:01 -07:00
|
|
|
let len = std::cmp::min(inner_buf.len(), buf.remaining());
|
|
|
|
|
buf.append(&inner_buf[..len]);
|
2020-04-02 23:18:52 +02:00
|
|
|
|
|
|
|
|
self.consume(len);
|
2020-08-13 20:15:01 -07:00
|
|
|
Poll::Ready(Ok(()))
|
2020-04-02 23:18:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 09:12:32 +02:00
|
|
|
impl<S, B, E> AsyncBufRead for StreamReader<S, B>
|
2020-04-02 23:18:52 +02:00
|
|
|
where
|
2020-09-08 09:12:32 +02:00
|
|
|
S: Stream<Item = Result<B, E>>,
|
2020-04-02 23:18:52 +02:00
|
|
|
B: Buf,
|
2020-09-08 09:12:32 +02:00
|
|
|
E: Into<std::io::Error>,
|
2020-04-02 23:18:52 +02:00
|
|
|
{
|
|
|
|
|
fn poll_fill_buf(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
|
|
|
|
|
loop {
|
|
|
|
|
if self.as_mut().has_chunk() {
|
|
|
|
|
// This unwrap is very sad, but it can't be avoided.
|
|
|
|
|
let buf = self.project().chunk.as_ref().unwrap().bytes();
|
|
|
|
|
return Poll::Ready(Ok(buf));
|
|
|
|
|
} else {
|
|
|
|
|
match self.as_mut().project().inner.poll_next(cx) {
|
|
|
|
|
Poll::Ready(Some(Ok(chunk))) => {
|
|
|
|
|
// Go around the loop in case the chunk is empty.
|
|
|
|
|
*self.as_mut().project().chunk = Some(chunk);
|
|
|
|
|
}
|
2020-09-08 09:12:32 +02:00
|
|
|
Poll::Ready(Some(Err(err))) => return Poll::Ready(Err(err.into())),
|
2020-04-02 23:18:52 +02:00
|
|
|
Poll::Ready(None) => return Poll::Ready(Ok(&[])),
|
|
|
|
|
Poll::Pending => return Poll::Pending,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn consume(self: Pin<&mut Self>, amt: usize) {
|
|
|
|
|
if amt > 0 {
|
|
|
|
|
self.project()
|
|
|
|
|
.chunk
|
|
|
|
|
.as_mut()
|
|
|
|
|
.expect("No chunk present")
|
|
|
|
|
.advance(amt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|