-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
216 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use futures_core::future::Future; | ||
use futures_core::task::{Context, Poll}; | ||
use futures_io::AsyncBufRead; | ||
use std::io; | ||
use std::pin::Pin; | ||
use std::str; | ||
use super::read_until::read_until_internal; | ||
|
||
/// Future for the [`read_line`](super::AsyncBufReadExt::read_line) method. | ||
#[derive(Debug)] | ||
pub struct ReadLine<'a, R: ?Sized + Unpin> { | ||
reader: &'a mut R, | ||
buf: &'a mut String, | ||
read: usize, | ||
} | ||
|
||
impl<R: ?Sized + Unpin> Unpin for ReadLine<'_, R> {} | ||
|
||
impl<'a, R: AsyncBufRead + ?Sized + Unpin> ReadLine<'a, R> { | ||
pub(super) fn new(reader: &'a mut R, buf: &'a mut String) -> Self { | ||
Self { reader, buf, read: 0 } | ||
} | ||
} | ||
|
||
struct Guard<'a> { buf: &'a mut Vec<u8>, len: usize } | ||
|
||
impl Drop for Guard<'_> { | ||
fn drop(&mut self) { | ||
unsafe { self.buf.set_len(self.len); } | ||
} | ||
} | ||
|
||
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, read } = &mut *self; | ||
unsafe { | ||
// safety: https://doc.rust-lang.org/src/std/io/mod.rs.html#310 | ||
let mut g = Guard { len: buf.len(), buf: buf.as_mut_vec() }; | ||
let poll = read_until_internal(Pin::new(reader), b'\n', g.buf, read, cx); | ||
if str::from_utf8(&g.buf[g.len..]).is_err() { | ||
let err = |_| Err(io::Error::new(io::ErrorKind::InvalidData, | ||
"stream did not contain valid UTF-8")); | ||
match poll { | ||
Poll::Ready(ret) => Poll::Ready(ret.and_then(err)), | ||
Poll::Pending => Poll::Ready(err(0)) | ||
} | ||
} else { | ||
g.len = g.buf.len(); | ||
poll | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use futures::executor::block_on; | ||
use futures::future::Future; | ||
use futures::io::{AsyncRead, AsyncBufRead, AsyncBufReadExt}; | ||
use futures::task::{Context, Poll}; | ||
use futures_test::task::noop_context; | ||
use std::cmp; | ||
use std::io::{self, Cursor}; | ||
use std::pin::Pin; | ||
|
||
#[test] | ||
fn read_line() { | ||
let mut buf = Cursor::new(&b"12"[..]); | ||
let mut v = String::new(); | ||
assert_eq!(block_on(buf.read_line(&mut v)).unwrap(), 2); | ||
assert_eq!(v, "12"); | ||
|
||
let mut buf = Cursor::new(&b"12\n\n"[..]); | ||
let mut v = String::new(); | ||
assert_eq!(block_on(buf.read_line(&mut v)).unwrap(), 3); | ||
assert_eq!(v, "12\n"); | ||
v.clear(); | ||
assert_eq!(block_on(buf.read_line(&mut v)).unwrap(), 1); | ||
assert_eq!(v, "\n"); | ||
v.clear(); | ||
assert_eq!(block_on(buf.read_line(&mut v)).unwrap(), 0); | ||
assert_eq!(v, ""); | ||
} | ||
|
||
fn run<F: Future + Unpin>(mut f: F) -> F::Output { | ||
let mut cx = noop_context(); | ||
loop { | ||
if let Poll::Ready(x) = Pin::new(&mut f).poll(&mut cx) { | ||
return x; | ||
} | ||
} | ||
} | ||
|
||
struct MaybePending<'a> { | ||
inner: &'a [u8], | ||
ready: bool, | ||
} | ||
|
||
impl<'a> MaybePending<'a> { | ||
fn new(inner: &'a [u8]) -> Self { | ||
Self { inner, ready: false } | ||
} | ||
} | ||
|
||
impl AsyncRead for MaybePending<'_> { | ||
fn poll_read(self: Pin<&mut Self>, _: &mut Context<'_>, _: &mut [u8]) | ||
-> Poll<io::Result<usize>> | ||
{ | ||
unimplemented!() | ||
} | ||
} | ||
|
||
impl AsyncBufRead for MaybePending<'_> { | ||
fn poll_fill_buf<'a>(mut self: Pin<&'a mut Self>, _: &mut Context<'_>) | ||
-> Poll<io::Result<&'a [u8]>> | ||
{ | ||
if self.ready { | ||
self.ready = false; | ||
if self.inner.is_empty() { return Poll::Ready(Ok(&[])) } | ||
let len = cmp::min(2, self.inner.len()); | ||
Poll::Ready(Ok(&self.inner[0..len])) | ||
} else { | ||
self.ready = true; | ||
Poll::Pending | ||
} | ||
} | ||
|
||
fn consume(mut self: Pin<&mut Self>, amt: usize) { | ||
self.inner = &self.inner[amt..]; | ||
} | ||
} | ||
|
||
#[test] | ||
fn maybe_ready() { | ||
let mut buf = MaybePending::new(&b"12"[..]); | ||
let mut v = String::new(); | ||
assert_eq!(run(buf.read_line(&mut v)).unwrap(), 2); | ||
assert_eq!(v, "12"); | ||
|
||
let mut buf = MaybePending::new(&b"12\n\n"[..]); | ||
let mut v = String::new(); | ||
assert_eq!(run(buf.read_line(&mut v)).unwrap(), 3); | ||
assert_eq!(v, "12\n"); | ||
v.clear(); | ||
assert_eq!(run(buf.read_line(&mut v)).unwrap(), 1); | ||
assert_eq!(v, "\n"); | ||
v.clear(); | ||
assert_eq!(run(buf.read_line(&mut v)).unwrap(), 0); | ||
assert_eq!(v, ""); | ||
v.clear(); | ||
assert_eq!(run(buf.read_line(&mut v)).unwrap(), 0); | ||
assert_eq!(v, ""); | ||
} |