summaryrefslogtreecommitdiffstats
path: root/tokio/src/io
diff options
context:
space:
mode:
authorTaiki Endo <te316e89@gmail.com>2019-07-15 15:23:42 +0900
committerSean McArthur <sean@seanmonstar.com>2019-07-15 11:52:13 -0700
commitab040bb498c96623f413efdc9fc16c46f72f9c63 (patch)
treed9ec2e165ee4968502f0b85b279b441531cdfa25 /tokio/src/io
parent0cfa120ba8c93822d6a84f2e7392008d84622010 (diff)
tokio: add AsyncBufReadExt::read_line
Diffstat (limited to 'tokio/src/io')
-rw-r--r--tokio/src/io/async_buf_read_ext.rs37
-rw-r--r--tokio/src/io/mod.rs1
-rw-r--r--tokio/src/io/read_line.rs70
3 files changed, 108 insertions, 0 deletions
diff --git a/tokio/src/io/async_buf_read_ext.rs b/tokio/src/io/async_buf_read_ext.rs
index 8cbfec45..e8178074 100644
--- a/tokio/src/io/async_buf_read_ext.rs
+++ b/tokio/src/io/async_buf_read_ext.rs
@@ -1,3 +1,4 @@
+use crate::io::read_line::{read_line, ReadLine};
use crate::io::read_until::{read_until, ReadUntil};
use tokio_io::AsyncBufRead;
@@ -29,6 +30,42 @@ pub trait AsyncBufReadExt: AsyncBufRead {
{
read_until(self, byte, buf)
}
+
+ /// Creates a future which will read all the bytes associated with this I/O
+ /// object into `buf` until a newline (the 0xA byte) or EOF is reached,
+ /// This method is the async equivalent to [`BufRead::read_line`](std::io::BufRead::read_line).
+ ///
+ /// This function will read bytes from the underlying stream until the
+ /// newline delimiter (the 0xA byte) or EOF is found. Once found, all bytes
+ /// up to, and including, the delimiter (if found) will be appended to
+ /// `buf`.
+ ///
+ /// The returned future will resolve to the number of bytes read once the read
+ /// operation is completed.
+ ///
+ /// In the case of an error the buffer and the object will be discarded, with
+ /// the error yielded.
+ ///
+ /// # Errors
+ ///
+ /// This function has the same error semantics as [`read_until`] and will
+ /// also return an error if the read bytes are not valid UTF-8. If an I/O
+ /// error is encountered then `buf` may contain some bytes already read in
+ /// the event that all data read so far was valid UTF-8.
+ ///
+ /// [`read_until`]: AsyncBufReadExt::read_until
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// unimplemented!();
+ /// ```
+ fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self>
+ where
+ Self: Unpin,
+ {
+ read_line(self, buf)
+ }
}
impl<R: AsyncBufRead + ?Sized> AsyncBufReadExt for R {}
diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs
index b185701a..bd1440ee 100644
--- a/tokio/src/io/mod.rs
+++ b/tokio/src/io/mod.rs
@@ -42,6 +42,7 @@ mod async_write_ext;
mod copy;
mod read;
mod read_exact;
+mod read_line;
mod read_to_end;
mod read_until;
mod write;
diff --git a/tokio/src/io/read_line.rs b/tokio/src/io/read_line.rs
new file mode 100644
index 00000000..987830d9
--- /dev/null
+++ b/tokio/src/io/read_line.rs
@@ -0,0 +1,70 @@
+use super::read_until::read_until_internal;
+use std::future::Future;
+use std::io;
+use std::mem;
+use std::pin::Pin;
+use std::str;
+use std::task::{Context, Poll};
+use tokio_io::AsyncBufRead;
+
+/// Future for the [`read_line`](crate::io::AsyncBufReadExt::read_line) method.
+#[derive(Debug)]
+#[must_use = "futures do nothing unless you `.await` or poll them"]
+pub struct ReadLine<'a, R: ?Sized + Unpin> {
+ reader: &'a mut R,
+ buf: &'a mut String,
+ bytes: Vec<u8>,
+ read: usize,
+}
+
+impl<R: ?Sized + Unpin> Unpin for ReadLine<'_, R> {}
+
+pub(crate) fn read_line<'a, R>(reader: &'a mut R, buf: &'a mut String) -> ReadLine<'a, R>
+where
+ R: AsyncBufRead + ?Sized + Unpin,
+{
+ ReadLine {
+ reader,
+ bytes: unsafe { mem::replace(buf.as_mut_vec(), Vec::new()) },
+ buf,
+ read: 0,
+ }
+}
+
+pub(super) fn read_line_internal<R: AsyncBufRead + ?Sized>(
+ reader: Pin<&mut R>,
+ cx: &mut Context<'_>,
+ buf: &mut String,
+ bytes: &mut Vec<u8>,
+ read: &mut usize,
+) -> Poll<io::Result<usize>> {
+ let ret = ready!(read_until_internal(reader, cx, b'\n', bytes, read));
+ if str::from_utf8(&bytes).is_err() {
+ Poll::Ready(ret.and_then(|_| {
+ Err(io::Error::new(
+ io::ErrorKind::InvalidData,
+ "stream did not contain valid UTF-8",
+ ))
+ }))
+ } else {
+ debug_assert!(buf.is_empty());
+ debug_assert_eq!(*read, 0);
+ // Safety: `bytes` is a valid UTF-8 because `str::from_utf8` returned `Ok`.
+ mem::swap(unsafe { buf.as_mut_vec() }, bytes);
+ Poll::Ready(ret)
+ }
+}
+
+impl<R: AsyncBufRead + ?Sized + Unpin> Future for ReadLine<'_, R> {
+ type Output = io::Result<usize>;
+
+ fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
+ let Self {
+ reader,
+ buf,
+ bytes,
+ read,
+ } = &mut *self;
+ read_line_internal(Pin::new(reader), cx, buf, bytes, read)
+ }
+}