From fb497aba4497c9ae30355136946ef5b6f27eb85b Mon Sep 17 00:00:00 2001 From: Evgeny Safronov Date: Mon, 12 Sep 2016 19:52:44 +0300 Subject: [PATCH 1/4] Add read_some free function to read some bytes --- src/io/mod.rs | 2 ++ src/io/read.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/io/read.rs diff --git a/src/io/mod.rs b/src/io/mod.rs index 93d37d56d..521dbe90f 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -35,6 +35,7 @@ mod copy; mod flush; mod read_exact; mod read_to_end; +mod read; mod split; mod window; mod write_all; @@ -42,6 +43,7 @@ pub use self::copy::{copy, Copy}; pub use self::flush::{flush, Flush}; pub use self::read_exact::{read_exact, ReadExact}; pub use self::read_to_end::{read_to_end, ReadToEnd}; +pub use self::read::{read_some, ReadSome}; pub use self::split::{ReadHalf, WriteHalf}; pub use self::window::Window; pub use self::write_all::{write_all, WriteAll}; diff --git a/src/io/read.rs b/src/io/read.rs new file mode 100644 index 000000000..c8e6b8b6f --- /dev/null +++ b/src/io/read.rs @@ -0,0 +1,65 @@ +use std::mem; +use std::io::Read; + +use futures::{Async, Future, Poll}; + +enum State { + Pending { + rd: R, + buf: T, + }, + Empty, +} + +fn eof() -> ::std::io::Error { + ::std::io::Error::new(::std::io::ErrorKind::UnexpectedEof, "unexpected EOF") +} + +/// Baz. +pub fn read_some(rd: R, buf: T) -> ReadSome + where R: Read, + T: AsMut<[u8]> +{ + ReadSome { + state: State::Pending { + rd: rd, + buf: buf, + } + } +} + +/// Bar. +pub struct ReadSome { + state: State, +} + +impl Future for ReadSome + where R: Read, + T: AsMut<[u8]> +{ + type Item = (R, T, usize); + type Error = ::std::io::Error; + + fn poll(&mut self) -> Poll<(R, T, usize), ::std::io::Error> { + let nread = match self.state { + State::Pending { ref mut rd, ref mut buf } => { + let buf = buf.as_mut(); + + match rd.read(&mut buf[..]) { + Ok(0) => return Err(eof()), + Ok(nread) => nread, + Err(ref err) if err.kind() == ::std::io::ErrorKind::WouldBlock => { + return Ok(Async::NotReady) + } + Err(err) => return Err(err.into()), + } + } + State::Empty => panic!("poll a ReadSome after it's done"), + }; + + match mem::replace(&mut self.state, State::Empty) { + State::Pending { rd, buf } => Ok((rd, buf, nread).into()), + State::Empty => panic!("invalid internal state"), + } + } +} From 4b3472ceae4b39a760e32a70f3d73b5e7aeabc0e Mon Sep 17 00:00:00 2001 From: Evgeny Safronov Date: Thu, 29 Sep 2016 12:19:20 +0300 Subject: [PATCH 2/4] refactor: rename, also make EOF as a valid result --- src/io/mod.rs | 2 +- src/io/read.rs | 25 +++++++++++-------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/io/mod.rs b/src/io/mod.rs index 521dbe90f..ecf004b4d 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -43,7 +43,7 @@ pub use self::copy::{copy, Copy}; pub use self::flush::{flush, Flush}; pub use self::read_exact::{read_exact, ReadExact}; pub use self::read_to_end::{read_to_end, ReadToEnd}; -pub use self::read::{read_some, ReadSome}; +pub use self::read::{read, ReadSome}; pub use self::split::{ReadHalf, WriteHalf}; pub use self::window::Window; pub use self::write_all::{write_all, WriteAll}; diff --git a/src/io/read.rs b/src/io/read.rs index c8e6b8b6f..07d5d87f8 100644 --- a/src/io/read.rs +++ b/src/io/read.rs @@ -11,24 +11,22 @@ enum State { Empty, } -fn eof() -> ::std::io::Error { - ::std::io::Error::new(::std::io::ErrorKind::UnexpectedEof, "unexpected EOF") -} - -/// Baz. -pub fn read_some(rd: R, buf: T) -> ReadSome +/// Tries to read some bytes directly into the given `buf` in asynchronous +/// manner, returning a future type. +/// +/// The returned future will resolve to both the I/O stream as well as the +/// buffer once the read operation is completed. +pub fn read(rd: R, buf: T) -> ReadSome where R: Read, T: AsMut<[u8]> { - ReadSome { - state: State::Pending { - rd: rd, - buf: buf, - } - } + ReadSome { state: State::Pending { rd: rd, buf: buf } } } -/// Bar. +/// A future which can be used to easily read available number of bytes to fill +/// a buffer. +/// +/// Created by the [`read`] function. pub struct ReadSome { state: State, } @@ -46,7 +44,6 @@ impl Future for ReadSome let buf = buf.as_mut(); match rd.read(&mut buf[..]) { - Ok(0) => return Err(eof()), Ok(nread) => nread, Err(ref err) if err.kind() == ::std::io::ErrorKind::WouldBlock => { return Ok(Async::NotReady) From 42bbe86cb62095a373c485717e6669e8ce15f1b5 Mon Sep 17 00:00:00 2001 From: Evgeny Safronov Date: Fri, 30 Sep 2016 13:14:41 +0300 Subject: [PATCH 3/4] refactor: rename ReadSome to Read Also `try_nb!` is used. --- src/io/mod.rs | 2 +- src/io/read.rs | 27 ++++++++------------------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/io/mod.rs b/src/io/mod.rs index ecf004b4d..3e44a6b4e 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -43,7 +43,7 @@ pub use self::copy::{copy, Copy}; pub use self::flush::{flush, Flush}; pub use self::read_exact::{read_exact, ReadExact}; pub use self::read_to_end::{read_to_end, ReadToEnd}; -pub use self::read::{read, ReadSome}; +pub use self::read::read; pub use self::split::{ReadHalf, WriteHalf}; pub use self::window::Window; pub use self::write_all::{write_all, WriteAll}; diff --git a/src/io/read.rs b/src/io/read.rs index 07d5d87f8..91cfbe284 100644 --- a/src/io/read.rs +++ b/src/io/read.rs @@ -1,7 +1,6 @@ use std::mem; -use std::io::Read; -use futures::{Async, Future, Poll}; +use futures::{Future, Poll}; enum State { Pending { @@ -16,23 +15,23 @@ enum State { /// /// The returned future will resolve to both the I/O stream as well as the /// buffer once the read operation is completed. -pub fn read(rd: R, buf: T) -> ReadSome - where R: Read, +pub fn read(rd: R, buf: T) -> Read + where R: ::std::io::Read, T: AsMut<[u8]> { - ReadSome { state: State::Pending { rd: rd, buf: buf } } + Read { state: State::Pending { rd: rd, buf: buf } } } /// A future which can be used to easily read available number of bytes to fill /// a buffer. /// /// Created by the [`read`] function. -pub struct ReadSome { +pub struct Read { state: State, } -impl Future for ReadSome - where R: Read, +impl Future for Read + where R: ::std::io::Read, T: AsMut<[u8]> { type Item = (R, T, usize); @@ -40,17 +39,7 @@ impl Future for ReadSome fn poll(&mut self) -> Poll<(R, T, usize), ::std::io::Error> { let nread = match self.state { - State::Pending { ref mut rd, ref mut buf } => { - let buf = buf.as_mut(); - - match rd.read(&mut buf[..]) { - Ok(nread) => nread, - Err(ref err) if err.kind() == ::std::io::ErrorKind::WouldBlock => { - return Ok(Async::NotReady) - } - Err(err) => return Err(err.into()), - } - } + State::Pending { ref mut rd, ref mut buf } => try_nb!(rd.read(&mut buf.as_mut()[..])), State::Empty => panic!("poll a ReadSome after it's done"), }; From f69f748470296f9c3fce66fcebb9456466a1c540 Mon Sep 17 00:00:00 2001 From: Evgeny Safronov Date: Fri, 30 Sep 2016 13:20:22 +0300 Subject: [PATCH 4/4] chore: fix panic string --- src/io/read.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/io/read.rs b/src/io/read.rs index 91cfbe284..6798280b7 100644 --- a/src/io/read.rs +++ b/src/io/read.rs @@ -40,7 +40,7 @@ impl Future for Read fn poll(&mut self) -> Poll<(R, T, usize), ::std::io::Error> { let nread = match self.state { State::Pending { ref mut rd, ref mut buf } => try_nb!(rd.read(&mut buf.as_mut()[..])), - State::Empty => panic!("poll a ReadSome after it's done"), + State::Empty => panic!("poll a Read after it's done"), }; match mem::replace(&mut self.state, State::Empty) {