From a0c3a33edad29adfb954ad8689ca8a0736e2750d Mon Sep 17 00:00:00 2001 From: Yazad Daruvala Date: Mon, 19 Jun 2017 01:16:45 -0700 Subject: [PATCH] refactor(http): Buffered::parse now returns Poll --- src/http/conn.rs | 11 +++-------- src/http/io.rs | 19 +++++++------------ 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/http/conn.rs b/src/http/conn.rs index e67de63d..bb2cee0b 100644 --- a/src/http/conn.rs +++ b/src/http/conn.rs @@ -47,11 +47,6 @@ where I: AsyncRead + AsyncWrite, } } - fn parse(&mut self) -> ::Result>> { - self.io.parse::() - } - - fn is_read_closed(&self) -> bool { self.state.is_read_closed() } @@ -79,9 +74,9 @@ where I: AsyncRead + AsyncWrite, debug_assert!(self.can_read_head()); trace!("Conn::read_head"); - let (version, head) = match self.parse() { - Ok(Some(head)) => (head.version, head), - Ok(None) => return Ok(Async::NotReady), + let (version, head) = match self.io.parse::() { + Ok(Async::Ready(head)) => (head.version, head), + Ok(Async::NotReady) => return Ok(Async::NotReady), Err(e) => { let must_respond_with_error = !self.state.is_idle(); self.state.close_read(); diff --git a/src/http/io.rs b/src/http/io.rs index 4adf26d6..64e6f053 100644 --- a/src/http/io.rs +++ b/src/http/io.rs @@ -55,12 +55,12 @@ impl Buffered { } } - pub fn parse(&mut self) -> ::Result>> { + pub fn parse(&mut self) -> Poll, ::Error> { loop { match try!(S::parse(&mut self.read_buf)) { Some(head) => { //trace!("parsed {} bytes out of {}", len, self.read_buf.len()); - return Ok(Some(head.0)) + return Ok(Async::Ready(head.0)) }, None => { if self.read_buf.capacity() >= MAX_BUFFER_SIZE { @@ -69,19 +69,13 @@ impl Buffered { } }, } - match self.read_from_io() { - Ok(0) => { + match try_nb!(self.read_from_io()) { + 0 => { trace!("parse eof"); //TODO: With Rust 1.14, this can be Error::from(ErrorKind) return Err(io::Error::new(io::ErrorKind::UnexpectedEof, ParseEof).into()); } - Ok(_) => {}, - Err(e) => match e.kind() { - io::ErrorKind::WouldBlock => { - return Ok(None); - }, - _ => return Err(e.into()) - } + _ => {}, } } } @@ -323,6 +317,7 @@ impl ::std::error::Error for ParseEof { } } +// TODO: Move tests to their own mod #[cfg(test)] use std::io::Read; @@ -358,6 +353,6 @@ fn test_parse_reads_until_blocked() { let mock = AsyncIo::new(MockBuf::wrap(raw.into()), raw.len()); let mut buffered = Buffered::new(mock); - assert_eq!(buffered.parse::().unwrap(), None); + assert_eq!(buffered.parse::().unwrap(), Async::NotReady); assert!(buffered.io.blocked()); }