2019-08-23 11:30:27 +00:00
|
|
|
use std::io::{self, Read};
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2020-01-22 11:49:08 +00:00
|
|
|
use std::sync::mpsc::Receiver;
|
|
|
|
|
2020-12-04 08:34:08 +00:00
|
|
|
use tokio::io::{AsyncRead, ReadBuf};
|
2020-06-22 14:44:13 +00:00
|
|
|
use futures::ready;
|
2019-01-20 08:38:28 +00:00
|
|
|
use futures::stream::Stream;
|
|
|
|
|
2020-01-20 11:52:22 +00:00
|
|
|
use crate::tools::runtime::block_in_place;
|
|
|
|
|
2020-01-22 11:49:08 +00:00
|
|
|
/// Wrapper struct to convert a Reader into a Stream
|
2019-08-23 11:30:27 +00:00
|
|
|
pub struct WrappedReaderStream<R: Read + Unpin> {
|
2019-01-20 08:38:28 +00:00
|
|
|
reader: R,
|
2019-01-20 10:01:18 +00:00
|
|
|
buffer: Vec<u8>,
|
2019-01-20 08:38:28 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 11:30:27 +00:00
|
|
|
impl <R: Read + Unpin> WrappedReaderStream<R> {
|
2019-01-20 08:38:28 +00:00
|
|
|
|
|
|
|
pub fn new(reader: R) -> Self {
|
2019-01-20 10:01:18 +00:00
|
|
|
let mut buffer = Vec::with_capacity(64*1024);
|
|
|
|
unsafe { buffer.set_len(buffer.capacity()); }
|
|
|
|
Self { reader, buffer }
|
2019-01-20 08:38:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 11:30:27 +00:00
|
|
|
impl<R: Read + Unpin> Stream for WrappedReaderStream<R> {
|
|
|
|
type Item = Result<Vec<u8>, io::Error>;
|
2019-01-20 08:38:28 +00:00
|
|
|
|
2019-08-23 11:30:27 +00:00
|
|
|
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Option<Self::Item>> {
|
|
|
|
let this = self.get_mut();
|
2019-12-12 14:27:07 +00:00
|
|
|
match block_in_place(|| this.reader.read(&mut this.buffer)) {
|
|
|
|
Ok(n) => {
|
2019-08-23 11:30:27 +00:00
|
|
|
if n == 0 {
|
|
|
|
// EOF
|
|
|
|
Poll::Ready(None)
|
2019-01-20 08:38:28 +00:00
|
|
|
} else {
|
2019-08-23 11:30:27 +00:00
|
|
|
Poll::Ready(Some(Ok(this.buffer[..n].to_vec())))
|
2019-01-20 08:38:28 +00:00
|
|
|
}
|
2019-08-23 11:30:27 +00:00
|
|
|
}
|
2019-12-12 14:27:07 +00:00
|
|
|
Err(err) => Poll::Ready(Some(Err(err))),
|
2019-01-20 08:38:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-03 08:05:45 +00:00
|
|
|
|
2020-06-22 14:44:13 +00:00
|
|
|
/// Wrapper struct to convert an AsyncReader into a Stream
|
|
|
|
pub struct AsyncReaderStream<R: AsyncRead + Unpin> {
|
|
|
|
reader: R,
|
|
|
|
buffer: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <R: AsyncRead + Unpin> AsyncReaderStream<R> {
|
|
|
|
|
|
|
|
pub fn new(reader: R) -> Self {
|
|
|
|
let mut buffer = Vec::with_capacity(64*1024);
|
|
|
|
unsafe { buffer.set_len(buffer.capacity()); }
|
|
|
|
Self { reader, buffer }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_buffer_size(reader: R, buffer_size: usize) -> Self {
|
|
|
|
let mut buffer = Vec::with_capacity(buffer_size);
|
|
|
|
unsafe { buffer.set_len(buffer.capacity()); }
|
|
|
|
Self { reader, buffer }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: AsyncRead + Unpin> Stream for AsyncReaderStream<R> {
|
|
|
|
type Item = Result<Vec<u8>, io::Error>;
|
|
|
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
|
|
|
let this = self.get_mut();
|
2020-12-04 08:34:08 +00:00
|
|
|
let mut read_buf = ReadBuf::new(&mut this.buffer);
|
|
|
|
match ready!(Pin::new(&mut this.reader).poll_read(cx, &mut read_buf)) {
|
|
|
|
Ok(()) => {
|
|
|
|
let n = read_buf.filled().len();
|
2020-06-22 14:44:13 +00:00
|
|
|
if n == 0 {
|
|
|
|
// EOF
|
|
|
|
Poll::Ready(None)
|
|
|
|
} else {
|
|
|
|
Poll::Ready(Some(Ok(this.buffer[..n].to_vec())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => Poll::Ready(Some(Err(err))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 11:49:08 +00:00
|
|
|
|
|
|
|
/// Wrapper struct to convert a channel Receiver into a Stream
|
|
|
|
pub struct StdChannelStream<T>(pub Receiver<T>);
|
|
|
|
|
|
|
|
impl<T> Stream for StdChannelStream<T> {
|
|
|
|
type Item = T;
|
|
|
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Option<Self::Item>> {
|
|
|
|
match block_in_place(|| self.0.recv()) {
|
|
|
|
Ok(data) => Poll::Ready(Some(data)),
|
|
|
|
Err(_) => Poll::Ready(None),// channel closed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 08:05:45 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use std::io;
|
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::Error;
|
2019-09-03 08:05:45 +00:00
|
|
|
use futures::stream::TryStreamExt;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_wrapped_stream_reader() -> Result<(), Error> {
|
2019-09-03 09:16:29 +00:00
|
|
|
crate::tools::runtime::main(async {
|
|
|
|
run_wrapped_stream_reader_test().await
|
|
|
|
})
|
2019-09-03 08:05:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct DummyReader(usize);
|
|
|
|
|
|
|
|
impl io::Read for DummyReader {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.0 += 1;
|
|
|
|
|
|
|
|
if self.0 >= 10 {
|
|
|
|
return Ok(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
std::ptr::write_bytes(buf.as_mut_ptr(), 0, buf.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_wrapped_stream_reader_test() -> Result<(), Error> {
|
|
|
|
let mut reader = super::WrappedReaderStream::new(DummyReader(0));
|
|
|
|
while let Some(_data) = reader.try_next().await? {
|
|
|
|
// just waiting
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|