-
Notifications
You must be signed in to change notification settings - Fork 605
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
141 additions
and
3 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
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,62 @@ | ||
use super::read_to_end::read_to_end_internal; | ||
use futures_core::future::Future; | ||
use futures_core::task::{Context, Poll}; | ||
use futures_io::AsyncRead; | ||
use std::pin::Pin; | ||
use std::vec::Vec; | ||
use std::{io, mem, str}; | ||
|
||
/// Future for the [`read_to_string`](super::AsyncReadExt::read_to_string) method. | ||
#[derive(Debug)] | ||
#[must_use = "futures do nothing unless you `.await` or poll them"] | ||
pub struct ReadToString<'a, R: ?Sized + Unpin> { | ||
reader: &'a mut R, | ||
buf: &'a mut String, | ||
bytes: Vec<u8>, | ||
} | ||
|
||
impl<R: ?Sized + Unpin> Unpin for ReadToString<'_, R> {} | ||
|
||
impl<'a, R: AsyncRead + ?Sized + Unpin> ReadToString<'a, R> { | ||
pub(super) fn new(reader: &'a mut R, buf: &'a mut String) -> Self { | ||
Self { | ||
reader, | ||
bytes: unsafe { mem::replace(buf.as_mut_vec(), Vec::new()) }, | ||
buf, | ||
} | ||
} | ||
} | ||
|
||
fn read_to_string_internal<R: AsyncRead + ?Sized>( | ||
reader: Pin<&mut R>, | ||
cx: &mut Context<'_>, | ||
buf: &mut String, | ||
bytes: &mut Vec<u8>, | ||
) -> Poll<io::Result<()>> { | ||
let ret = ready!(read_to_end_internal(reader, cx, bytes)); | ||
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()); | ||
// 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<A> Future for ReadToString<'_, A> | ||
where | ||
A: AsyncRead + ?Sized + Unpin, | ||
{ | ||
type Output = io::Result<()>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let Self { reader, buf, bytes } = &mut *self; | ||
read_to_string_internal(Pin::new(reader), cx, buf, bytes) | ||
} | ||
} |
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,46 @@ | ||
use futures::executor::block_on; | ||
use futures::future::{Future, FutureExt}; | ||
use futures::stream::{self, StreamExt, TryStreamExt}; | ||
use futures::io::AsyncReadExt; | ||
use futures::task::Poll; | ||
use futures_test::io::AsyncReadTestExt; | ||
use futures_test::task::noop_context; | ||
use std::io::Cursor; | ||
|
||
#[test] | ||
fn read_to_string() { | ||
let mut c = Cursor::new(&b""[..]); | ||
let mut v = String::new(); | ||
assert!(block_on(c.read_to_string(&mut v)).is_ok()); | ||
assert_eq!(v, ""); | ||
|
||
let mut c = Cursor::new(&b"1"[..]); | ||
let mut v = String::new(); | ||
assert!(block_on(c.read_to_string(&mut v)).is_ok()); | ||
assert_eq!(v, "1"); | ||
|
||
let mut c = Cursor::new(&b"\xff"[..]); | ||
let mut v = String::new(); | ||
assert!(block_on(c.read_to_string(&mut v)).is_err()); | ||
} | ||
|
||
fn run<F: Future + Unpin>(mut f: F) -> F::Output { | ||
let mut cx = noop_context(); | ||
loop { | ||
if let Poll::Ready(x) = f.poll_unpin(&mut cx) { | ||
return x; | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn interleave_pending() { | ||
let mut buf = stream::iter(vec![&b"12"[..], &b"33"[..], &b"3"[..]]) | ||
.map(Ok) | ||
.into_async_read() | ||
.interleave_pending(); | ||
|
||
let mut v = String::new(); | ||
assert!(run(buf.read_to_string(&mut v)).is_ok()); | ||
assert_eq!(v, "12333"); | ||
} |