diff options
author | Eli W. Hunter <42009212+elihunter173@users.noreply.github.com> | 2020-07-23 23:54:12 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-23 23:54:12 -0400 |
commit | 6cd1d7f93bd6f150341582a1b54087cefffdbf87 (patch) | |
tree | 88c109ec79e679d5aa041b20f074cf7b57d97cda /src/read.rs | |
parent | a4cd2185976ad56b880d5a10374c4dee6d116e6a (diff) |
Async/Await Support (continuation of #191) (#229)
* it builds!
* remove unused dependencies
* bump dependencies
* reimplement 'exec' endpoint
* update a few more examples
* update remaining examples
* fix doc tests, remove unused 'read' module
* remove feature-gated async closures
* split futures dependency to just 'futures-util'
* update version and readme
* make functions accepting Body generic over Into<Body> again
* update changelog
* reinstate 'unix-socket' feature
* reinstate 'attach' endpoint
* fix clippy lints
* fix documentation typo
* fix container copyfrom/into implementations
* add convenience methods for TtyChunk struct
* remove 'main' from code example to silence clippy lint
* Update hyper to 0.13.1
* Add Send bounds to TtyWriter
* Appease clippy
* Fix examples
* Update issue in changelog
Co-authored-by: Daniel Eades <danieleades@hotmail.com>
Co-authored-by: Marc Schreiber <marc.schreiber@aixigo.de>
Diffstat (limited to 'src/read.rs')
-rw-r--r-- | src/read.rs | 105 |
1 files changed, 0 insertions, 105 deletions
diff --git a/src/read.rs b/src/read.rs deleted file mode 100644 index b9dc5ef..0000000 --- a/src/read.rs +++ /dev/null @@ -1,105 +0,0 @@ -use crate::errors::Error; -use futures::{Async, Stream}; -use hyper::Chunk; -use std::{ - cmp, - io::{self, Read}, -}; -use tokio_io::AsyncRead; - -/* - * The following is taken from - * https://github.com/ferristseng/rust-ipfs-api/blob/master/ipfs-api/src/read.rs. - * TODO: see with upstream author to move to a separate crate. - */ - -/// The state of a stream returning Chunks. -/// -enum ReadState { - /// A chunk is ready to be read from. - /// - Ready(Chunk, usize), - - /// The next chunk isn't ready yet. - /// - NotReady, -} - -/// Reads from a stream of chunks asynchronously. -/// -pub struct StreamReader<S> { - stream: S, - state: ReadState, -} - -impl<S> StreamReader<S> -where - S: Stream<Item = Chunk, Error = Error>, -{ - #[inline] - pub fn new(stream: S) -> StreamReader<S> { - StreamReader { - stream, - state: ReadState::NotReady, - } - } -} - -impl<S> Read for StreamReader<S> -where - S: Stream<Item = Chunk, Error = Error>, -{ - fn read( - &mut self, - buf: &mut [u8], - ) -> io::Result<usize> { - loop { - let ret; - - match self.state { - // Stream yielded a Chunk to read. - // - ReadState::Ready(ref mut chunk, ref mut pos) => { - let chunk_start = *pos; - let len = cmp::min(buf.len(), chunk.len() - chunk_start); - let chunk_end = chunk_start + len; - - buf[..len].copy_from_slice(&chunk[chunk_start..chunk_end]); - *pos += len; - - if *pos == chunk.len() { - ret = len; - } else { - return Ok(len); - } - } - // Stream is not ready, and a Chunk needs to be read. - // - ReadState::NotReady => { - match self.stream.poll() { - // Polling stream yielded a Chunk that can be read from. - // - Ok(Async::Ready(Some(chunk))) => { - self.state = ReadState::Ready(chunk, 0); - - continue; - } - // Polling stream yielded EOF. - // - Ok(Async::Ready(None)) => return Ok(0), - // Stream could not be read from. - // - Ok(Async::NotReady) => return Err(io::ErrorKind::WouldBlock.into()), - Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e.to_string())), - } - } - } - - self.state = ReadState::NotReady; - - return Ok(ret); - } - } -} - -impl<S> AsyncRead for StreamReader<S> where S: Stream<Item = Chunk, Error = Error> {} |