From 6c4982bd7cd964087f9fc304a04133e8ec37c38f Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 20 Nov 2021 16:15:05 +0100 Subject: [PATCH] proxmox-async: remove duplicate src/stream/wrapped_reader_stream.rs Signed-off-by: Dietmar Maurer --- proxmox-async/src/stream/mod.rs | 3 - .../src/stream/wrapped_reader_stream.rs | 82 ------------------- 2 files changed, 85 deletions(-) delete mode 100644 proxmox-async/src/stream/wrapped_reader_stream.rs diff --git a/proxmox-async/src/stream/mod.rs b/proxmox-async/src/stream/mod.rs index ad22e016..26f9e516 100644 --- a/proxmox-async/src/stream/mod.rs +++ b/proxmox-async/src/stream/mod.rs @@ -5,6 +5,3 @@ pub use async_channel_writer::AsyncChannelWriter; mod async_reader_stream; pub use async_reader_stream::AsyncReaderStream; - -mod wrapped_reader_stream; -pub use wrapped_reader_stream::WrappedReaderStream; diff --git a/proxmox-async/src/stream/wrapped_reader_stream.rs b/proxmox-async/src/stream/wrapped_reader_stream.rs deleted file mode 100644 index d9603a23..00000000 --- a/proxmox-async/src/stream/wrapped_reader_stream.rs +++ /dev/null @@ -1,82 +0,0 @@ -use std::io::{self, Read}; -use std::pin::Pin; -use std::task::{Context, Poll}; - -use futures::stream::Stream; - -use crate::runtime::block_in_place; - -/// Wrapper struct to convert a Reader into a Stream -pub struct WrappedReaderStream { - reader: R, - buffer: Vec, -} - -impl WrappedReaderStream { - - pub fn new(reader: R) -> Self { - let mut buffer = Vec::with_capacity(64*1024); - unsafe { buffer.set_len(buffer.capacity()); } - Self { reader, buffer } - } -} - -impl Stream for WrappedReaderStream { - type Item = Result, io::Error>; - - fn poll_next(self: Pin<&mut Self>, _cx: &mut Context) -> Poll> { - let this = self.get_mut(); - match block_in_place(|| this.reader.read(&mut this.buffer)) { - Ok(n) => { - if n == 0 { - // EOF - Poll::Ready(None) - } else { - Poll::Ready(Some(Ok(this.buffer[..n].to_vec()))) - } - } - Err(err) => Poll::Ready(Some(Err(err))), - } - } -} - -#[cfg(test)] -mod test { - use std::io; - - use anyhow::Error; - use futures::stream::TryStreamExt; - - #[test] - fn test_wrapped_stream_reader() -> Result<(), Error> { - crate::runtime::main(async { - run_wrapped_stream_reader_test().await - }) - } - - struct DummyReader(usize); - - impl io::Read for DummyReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -}