2019-08-23 11:30:27 +00:00
|
|
|
use std::io::{self, Read};
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
|
|
|
use tokio_executor::threadpool::blocking;
|
2019-01-20 08:38:28 +00:00
|
|
|
use futures::stream::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();
|
|
|
|
match blocking(|| this.reader.read(&mut this.buffer)) {
|
|
|
|
Poll::Ready(Ok(Ok(n))) => {
|
|
|
|
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
|
|
|
}
|
|
|
|
Poll::Ready(Ok(Err(err))) => Poll::Ready(Some(Err(err))),
|
|
|
|
Poll::Ready(Err(err)) => Poll::Ready(Some(Err(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
err.to_string(),
|
|
|
|
)))),
|
|
|
|
Poll::Pending => Poll::Pending,
|
2019-01-20 08:38:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|